github.com/kubewharf/katalyst-core@v0.5.3/pkg/client/control/kcc.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 control
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  
    25  	"github.com/kubewharf/katalyst-api/pkg/apis/config/v1alpha1"
    26  	clientset "github.com/kubewharf/katalyst-api/pkg/client/clientset/versioned"
    27  )
    28  
    29  // KCCControl is used to update KatalystCustomConfig
    30  // todo: use patch instead of update to avoid conflict
    31  type KCCControl interface {
    32  	// UpdateKCC is used to update the changes for KCC spec and metadata contents
    33  	UpdateKCC(ctx context.Context, kcc *v1alpha1.KatalystCustomConfig,
    34  		opts metav1.UpdateOptions) (*v1alpha1.KatalystCustomConfig, error)
    35  
    36  	// UpdateKCCStatus is used to update the change for KCC status
    37  	UpdateKCCStatus(ctx context.Context, kcc *v1alpha1.KatalystCustomConfig,
    38  		opts metav1.UpdateOptions) (*v1alpha1.KatalystCustomConfig, error)
    39  }
    40  
    41  type DummyKCCControl struct{}
    42  
    43  func (d DummyKCCControl) UpdateKCC(_ context.Context, kcc *v1alpha1.KatalystCustomConfig,
    44  	_ metav1.UpdateOptions,
    45  ) (*v1alpha1.KatalystCustomConfig, error) {
    46  	return kcc, nil
    47  }
    48  
    49  func (d DummyKCCControl) UpdateKCCStatus(_ context.Context, kcc *v1alpha1.KatalystCustomConfig,
    50  	_ metav1.UpdateOptions,
    51  ) (*v1alpha1.KatalystCustomConfig, error) {
    52  	return kcc, nil
    53  }
    54  
    55  type RealKCCControl struct {
    56  	client clientset.Interface
    57  }
    58  
    59  func (r *RealKCCControl) UpdateKCC(ctx context.Context, kcc *v1alpha1.KatalystCustomConfig,
    60  	opts metav1.UpdateOptions,
    61  ) (*v1alpha1.KatalystCustomConfig, error) {
    62  	if kcc == nil {
    63  		return nil, fmt.Errorf("can't update a nil KCC")
    64  	}
    65  
    66  	return r.client.ConfigV1alpha1().KatalystCustomConfigs(kcc.Namespace).Update(ctx, kcc, opts)
    67  }
    68  
    69  func (r *RealKCCControl) UpdateKCCStatus(ctx context.Context, kcc *v1alpha1.KatalystCustomConfig,
    70  	opts metav1.UpdateOptions,
    71  ) (*v1alpha1.KatalystCustomConfig, error) {
    72  	if kcc == nil {
    73  		return nil, fmt.Errorf("can't update a nil KCC's status")
    74  	}
    75  
    76  	return r.client.ConfigV1alpha1().KatalystCustomConfigs(kcc.Namespace).UpdateStatus(ctx, kcc, opts)
    77  }
    78  
    79  func NewRealKCCControl(client clientset.Interface) *RealKCCControl {
    80  	return &RealKCCControl{
    81  		client: client,
    82  	}
    83  }