kubernetes/metadata/replicaset.go (80 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 ( "fmt" appsv1 "k8s.io/api/apps/v1" 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" ) const resourceType = "replicaset" type replicaset struct { store cache.Store resource *Resource } // NewReplicasetMetadataGenerator creates a metagen for replicaset resources func NewReplicasetMetadataGenerator(cfg *config.C, replicasets cache.Store, client k8s.Interface) MetaGen { return &replicaset{ resource: NewResourceMetadataGenerator(cfg, client), store: replicasets, } } // Generate generates replicaset 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 (rs *replicaset) Generate(obj kubernetes.Resource, opts ...FieldOptions) mapstr.M { ecsFields := rs.GenerateECS(obj) meta := mapstr.M{ "kubernetes": rs.GenerateK8s(obj, opts...), } meta.DeepUpdate(ecsFields) return meta } // GenerateECS generates replicaset ECS metadata from a resource object func (rs *replicaset) GenerateECS(obj kubernetes.Resource) mapstr.M { return rs.resource.GenerateECS(obj) } // GenerateK8s generates replicaset metadata from a resource object func (rs *replicaset) GenerateK8s(obj kubernetes.Resource, opts ...FieldOptions) mapstr.M { _, ok := obj.(metav1.Object) if !ok { return nil } meta := rs.resource.GenerateK8s(resourceType, obj, opts...) return meta } // GenerateFromName generates replicaset metadata from a replicaset name func (rs *replicaset) GenerateFromName(name string, opts ...FieldOptions) mapstr.M { if rs.store == nil { return nil } if obj, ok, _ := rs.store.GetByKey(name); ok { res, ok := obj.(kubernetes.Resource) if !ok { return nil } return rs.GenerateK8s(res, opts...) } return nil } // RemoveUnnecessaryReplicaSetData removes all data from a ReplicaSet resource, except what we need to compute // Pod metadata. This function works for both ReplicaSet and PartialObjectMetadata. func RemoveUnnecessaryReplicaSetData(obj interface{}) (interface{}, error) { switch old := obj.(type) { case *appsv1.ReplicaSet: transformed := &appsv1.ReplicaSet{ ObjectMeta: kubernetes.ObjectMeta{ Name: old.GetName(), Namespace: old.GetNamespace(), OwnerReferences: old.GetOwnerReferences(), ResourceVersion: old.GetResourceVersion(), }, } return transformed, nil case *metav1.PartialObjectMetadata: transformed := &metav1.PartialObjectMetadata{ ObjectMeta: kubernetes.ObjectMeta{ Name: old.GetName(), Namespace: old.GetNamespace(), OwnerReferences: old.GetOwnerReferences(), ResourceVersion: old.GetResourceVersion(), }, } return transformed, nil default: return nil, fmt.Errorf("obj of type %T neither a ReplicaSet nor a PartialObjectMetadata", obj) } }