github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/commands/alpha/rpkg/util/common.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 util 16 17 import ( 18 "context" 19 "fmt" 20 21 fnsdk "github.com/GoogleContainerTools/kpt-functions-sdk/go/fn" 22 kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1" 23 api "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1" 24 "sigs.k8s.io/controller-runtime/pkg/client" 25 ) 26 27 const ( 28 ResourceVersionAnnotation = "internal.kpt.dev/resource-version" 29 ) 30 31 func PackageAlreadyExists(ctx context.Context, c client.Client, repository, packageName, namespace string) (bool, error) { 32 // only the first package revision can be created from init or clone, so 33 // we need to check that the package doesn't already exist. 34 packageRevisionList := api.PackageRevisionList{} 35 if err := c.List(ctx, &packageRevisionList, &client.ListOptions{ 36 Namespace: namespace, 37 }); err != nil { 38 return false, err 39 } 40 for _, pr := range packageRevisionList.Items { 41 if pr.Spec.RepositoryName == repository && pr.Spec.PackageName == packageName { 42 return true, nil 43 } 44 } 45 return false, nil 46 } 47 48 func GetResourceFileKubeObject(prr *api.PackageRevisionResources, file, kind, name string) (*fnsdk.KubeObject, error) { 49 if prr.Spec.Resources == nil { 50 return nil, fmt.Errorf("nil resources found for PackageRevisionResources '%s/%s'", prr.Namespace, prr.Name) 51 } 52 53 if _, ok := prr.Spec.Resources[file]; !ok { 54 return nil, fmt.Errorf("%q not found in PackageRevisionResources '%s/%s'", file, prr.Namespace, prr.Name) 55 } 56 57 ko, err := fnsdk.ParseKubeObject([]byte(prr.Spec.Resources[file])) 58 if err != nil { 59 return nil, fmt.Errorf("failed to parse %q of PackageRevisionResources %s/%s: %w", file, prr.Namespace, prr.Name, err) 60 } 61 if kind != "" && ko.GetKind() != kind { 62 return nil, fmt.Errorf("%q does not contain kind %q in PackageRevisionResources '%s/%s'", file, kind, prr.Namespace, prr.Name) 63 } 64 if name != "" && ko.GetName() != name { 65 return nil, fmt.Errorf("%q does not contain resource named %q in PackageRevisionResources '%s/%s'", file, name, prr.Namespace, prr.Name) 66 } 67 68 return ko, nil 69 } 70 71 func GetResourceVersion(prr *api.PackageRevisionResources) (string, error) { 72 ko, err := GetResourceFileKubeObject(prr, kptfilev1.RevisionMetaDataFileName, kptfilev1.RevisionMetaDataKind, "") 73 if err != nil { 74 return "", err 75 } 76 rv, _, _ := ko.NestedString("metadata", "resourceVersion") 77 return rv, nil 78 } 79 80 func AddRevisionMetadata(prr *api.PackageRevisionResources) error { 81 kptMetaDataKo := fnsdk.NewEmptyKubeObject() 82 kptMetaDataKo.SetAPIVersion(prr.APIVersion) 83 kptMetaDataKo.SetKind(kptfilev1.RevisionMetaDataKind) 84 if err := kptMetaDataKo.SetNestedField(prr.GetObjectMeta(), "metadata"); err != nil { 85 return fmt.Errorf("cannot set metadata: %v", err) 86 } 87 prr.Spec.Resources[kptfilev1.RevisionMetaDataFileName] = kptMetaDataKo.String() 88 89 return nil 90 } 91 92 func RemoveRevisionMetadata(prr *api.PackageRevisionResources) error { 93 delete(prr.Spec.Resources, kptfilev1.RevisionMetaDataFileName) 94 return nil 95 }