kubernetes/metadata/job.go (51 lines of code) (raw):
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package metadata
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8s "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"github.com/elastic/elastic-agent-autodiscover/kubernetes"
"github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/mapstr"
)
type job struct {
store cache.Store
resource *Resource
}
// NewJobMetadataGenerator creates a metagen for job resources
func NewJobMetadataGenerator(cfg *config.C, jobs cache.Store, client k8s.Interface) MetaGen {
return &job{
resource: NewResourceMetadataGenerator(cfg, client),
store: jobs,
}
}
// Generate generates job metadata from a resource object
// Metadata map is in the following form:
//
// {
// "kubernetes": {},
// "some.ecs.field": "asdf"
// }
//
// All Kubernetes fields that need to be stored under kuberentes. prefix are populetad by
// GenerateK8s method while fields that are part of ECS are generated by GenerateECS method
func (jb *job) Generate(obj kubernetes.Resource, opts ...FieldOptions) mapstr.M {
ecsFields := jb.GenerateECS(obj)
meta := mapstr.M{
"kubernetes": jb.GenerateK8s(obj, opts...),
}
meta.DeepUpdate(ecsFields)
return meta
}
// GenerateECS generates job ECS metadata from a resource object
func (jb *job) GenerateECS(obj kubernetes.Resource) mapstr.M {
return jb.resource.GenerateECS(obj)
}
// GenerateK8s generates job metadata from a resource object
func (jb *job) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) mapstr.M {
_, ok := obj.(metav1.Object)
if !ok {
return nil
}
meta := jb.resource.GenerateK8s("job", obj, opts...)
return meta
}
// GenerateFromName generates job metadata from a namespace name
func (jb *job) GenerateFromName(name string, opts ...FieldOptions) mapstr.M {
if jb.store == nil {
return nil
}
if obj, ok, _ := jb.store.GetByKey(name); ok {
res, ok := obj.(kubernetes.Resource)
if !ok {
return nil
}
return jb.GenerateK8s(res, opts...)
}
return nil
}