github.com/kubewharf/katalyst-core@v0.5.3/pkg/webhook/mutating/pod/spd_ref_mutator.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package pod
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	core "k8s.io/api/core/v1"
    24  	"k8s.io/apimachinery/pkg/runtime/schema"
    25  	"k8s.io/client-go/tools/cache"
    26  	"k8s.io/klog/v2"
    27  
    28  	workloadlister "github.com/kubewharf/katalyst-api/pkg/client/listers/workload/v1alpha1"
    29  	apiconsts "github.com/kubewharf/katalyst-api/pkg/consts"
    30  	katalystutil "github.com/kubewharf/katalyst-core/pkg/util"
    31  )
    32  
    33  type WebhookPodSPDReferenceMutator struct {
    34  	ctx context.Context
    35  
    36  	spdIndexer cache.Indexer
    37  
    38  	spdLister      workloadlister.ServiceProfileDescriptorLister
    39  	workloadLister map[schema.GroupVersionKind]cache.GenericLister
    40  }
    41  
    42  // NewWebhookPodSPDReferenceMutator will maintain spd reference annotations for pod
    43  func NewWebhookPodSPDReferenceMutator(
    44  	ctx context.Context,
    45  	spdIndexer cache.Indexer,
    46  	spdLister workloadlister.ServiceProfileDescriptorLister,
    47  	workloadLister map[schema.GroupVersionKind]cache.GenericLister,
    48  ) *WebhookPodSPDReferenceMutator {
    49  	s := WebhookPodSPDReferenceMutator{
    50  		ctx:            ctx,
    51  		spdIndexer:     spdIndexer,
    52  		spdLister:      spdLister,
    53  		workloadLister: workloadLister,
    54  	}
    55  	return &s
    56  }
    57  
    58  func (s *WebhookPodSPDReferenceMutator) MutatePod(pod *core.Pod, namespace string) (mutated bool, err error) {
    59  	if pod == nil {
    60  		err := fmt.Errorf("pod is nil")
    61  		klog.Error(err.Error())
    62  		return false, err
    63  	}
    64  
    65  	pod.Namespace = namespace
    66  	spd, err := katalystutil.GetSPDForPod(pod, s.spdIndexer, s.workloadLister, s.spdLister, true)
    67  	if err != nil || spd == nil {
    68  		klog.Warningf("didn't to find spd of pod %v/%v, err: %v", pod.Namespace, pod.Name, err)
    69  		return false, nil
    70  	}
    71  
    72  	if pod.Annotations == nil {
    73  		pod.Annotations = make(map[string]string)
    74  	}
    75  	pod.Annotations[apiconsts.PodAnnotationSPDNameKey] = spd.Name
    76  	return true, nil
    77  }