github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/internal/util/porch/approval.go (about) 1 // Copyright 2022 Google LLC 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 porch 16 17 import ( 18 "context" 19 "fmt" 20 21 "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1" 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 "k8s.io/apimachinery/pkg/runtime" 24 "k8s.io/client-go/rest" 25 "sigs.k8s.io/controller-runtime/pkg/client" 26 ) 27 28 func UpdatePackageRevisionApproval(ctx context.Context, client rest.Interface, key client.ObjectKey, new v1alpha1.PackageRevisionLifecycle) error { 29 scheme := runtime.NewScheme() 30 if err := v1alpha1.SchemeBuilder.AddToScheme(scheme); err != nil { 31 return err 32 } 33 34 codec := runtime.NewParameterCodec(scheme) 35 var pr v1alpha1.PackageRevision 36 if err := client.Get(). 37 Namespace(key.Namespace). 38 Resource("packagerevisions"). 39 Name(key.Name). 40 VersionedParams(&metav1.GetOptions{}, codec). 41 Do(ctx). 42 Into(&pr); err != nil { 43 return err 44 } 45 46 switch lifecycle := pr.Spec.Lifecycle; lifecycle { 47 case v1alpha1.PackageRevisionLifecycleProposed: 48 // ok 49 case new: 50 // already correct value 51 return nil 52 default: 53 return fmt.Errorf("cannot change approval from %s to %s", lifecycle, new) 54 } 55 56 // Approve - change the package revision kind to "final". 57 pr.Spec.Lifecycle = new 58 59 opts := metav1.UpdateOptions{} 60 result := &v1alpha1.PackageRevision{} 61 if err := client.Put(). 62 Namespace(pr.Namespace). 63 Resource("packagerevisions"). 64 Name(pr.Name). 65 SubResource("approval"). 66 VersionedParams(&opts, codec). 67 Body(&pr). 68 Do(ctx). 69 Into(result); err != nil { 70 return err 71 } 72 return nil 73 }