github.com/GoogleContainerTools/kpt@v1.0.0-beta.50.0.20240520170205-c25345ffcbee/commands/alpha/rollouts/rolloutsclient/client.go (about) 1 // Copyright 2023 The kpt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package rolloutsclient 16 17 import ( 18 "context" 19 "fmt" 20 "time" 21 22 rolloutsapi "github.com/GoogleContainerTools/kpt/rollouts/api/v1alpha1" 23 coreapi "k8s.io/api/core/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/runtime" 26 "k8s.io/apimachinery/pkg/types" 27 "k8s.io/client-go/rest" 28 "k8s.io/klog/v2" 29 "sigs.k8s.io/cli-utils/pkg/flowcontrol" 30 "sigs.k8s.io/controller-runtime/pkg/client" 31 "sigs.k8s.io/controller-runtime/pkg/client/config" 32 ) 33 34 // Client implements client for the rollouts API. 35 type Client struct { 36 client client.Client 37 } 38 39 func New() (*Client, error) { 40 scheme, err := createScheme() 41 if err != nil { 42 return nil, err 43 } 44 45 config := useServerSideThrottling(config.GetConfigOrDie()) 46 cl, err := client.New(config, client.Options{ 47 Scheme: scheme, 48 }) 49 if err != nil { 50 return nil, err 51 } 52 return &Client{client: cl}, nil 53 } 54 55 func createScheme() (*runtime.Scheme, error) { 56 scheme := runtime.NewScheme() 57 58 for _, api := range (runtime.SchemeBuilder{ 59 rolloutsapi.AddToScheme, 60 coreapi.AddToScheme, 61 metav1.AddMetaToScheme, 62 }) { 63 if err := api(scheme); err != nil { 64 return nil, err 65 } 66 } 67 return scheme, nil 68 } 69 70 func (rlc *Client) List(ctx context.Context, ns string) (*rolloutsapi.RolloutList, error) { 71 var opts []client.ListOption 72 if ns != "" { 73 opts = append(opts, client.InNamespace(ns)) 74 } 75 76 rollouts := &rolloutsapi.RolloutList{} 77 if err := rlc.client.List(ctx, rollouts, opts...); err != nil { 78 return nil, err 79 } 80 81 return rollouts, nil 82 } 83 84 func (rlc *Client) Get(ctx context.Context, ns, name string) (*rolloutsapi.Rollout, error) { 85 if name == "" { 86 return nil, fmt.Errorf("must provide rollout name") 87 } 88 89 key := types.NamespacedName{ 90 Namespace: ns, 91 Name: name, 92 } 93 rollout := &rolloutsapi.Rollout{} 94 if err := rlc.client.Get(ctx, key, rollout); err != nil { 95 return nil, err 96 } 97 98 return rollout, nil 99 } 100 101 func (rlc *Client) Update(ctx context.Context, rollout *rolloutsapi.Rollout) error { 102 return rlc.client.Update(ctx, rollout) 103 } 104 105 func useServerSideThrottling(config *rest.Config) *rest.Config { 106 // Timeout if the query takes too long 107 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 108 defer cancel() 109 110 enabled, err := flowcontrol.IsEnabled(ctx, config) 111 if err != nil { 112 klog.Infof("Couldn't gather flow control configuration from the API apiserver (assuming it is not enabled): %v\n", err) 113 } 114 115 if enabled { 116 config.QPS = -1 117 config.Burst = -1 118 } 119 120 return config 121 }