@@ -18,6 +18,7 @@ import (
1818 "fmt"
1919 "reflect"
2020 "strconv"
21+ "strings"
2122
2223 "github.com/GoogleContainerTools/kpt-functions-sdk/go/fn/internal"
2324 "sigs.k8s.io/kustomize/kyaml/kio/kioutil"
@@ -690,6 +691,18 @@ func (o *KubeObject) GetAnnotation(k string) string {
690691 return v
691692}
692693
694+ // HasAnnotations returns whether the KubeObject has the provided annotations
695+ func (o * KubeObject ) HasAnnotations (annotations map [string ]string ) bool {
696+ kubeObjectLabels := o .GetAnnotations ()
697+ for k , v := range annotations {
698+ kubeObjectValue , found := kubeObjectLabels [k ]
699+ if ! found || kubeObjectValue != v {
700+ return false
701+ }
702+ }
703+ return true
704+ }
705+
693706// RemoveAnnotationsIfEmpty removes the annotations field when it has zero annotations.
694707func (o * KubeObject ) RemoveAnnotationsIfEmpty () error {
695708 annotations , found , err := o .obj .GetNestedStringMap ("metadata" , "annotations" )
@@ -721,6 +734,18 @@ func (o *KubeObject) GetLabels() map[string]string {
721734 return v
722735}
723736
737+ // HasLabels returns whether the KubeObject has the provided labels
738+ func (o * KubeObject ) HasLabels (labels map [string ]string ) bool {
739+ kubeObjectLabels := o .GetLabels ()
740+ for k , v := range labels {
741+ kubeObjectValue , found := kubeObjectLabels [k ]
742+ if ! found || kubeObjectValue != v {
743+ return false
744+ }
745+ }
746+ return true
747+ }
748+
724749func (o * KubeObject ) PathAnnotation () string {
725750 anno := o .GetAnnotation (kioutil .PathAnnotation )
726751 return anno
@@ -759,6 +784,107 @@ func (o KubeObjects) Less(i, j int) bool {
759784 return idStrI < idStrJ
760785}
761786
787+ func (o KubeObjects ) String () string {
788+ var elems []string
789+ for _ , obj := range o {
790+ elems = append (elems , strings .TrimSpace (obj .String ()))
791+ }
792+ return strings .Join (elems , "\n ---\n " )
793+ }
794+
795+ // Select will return the subset of objects in KubeObjects such that f(object) returns 'true'.
796+ func (o KubeObjects ) Select (f func (o * KubeObject ) bool ) KubeObjects {
797+ var result KubeObjects
798+ for _ , obj := range o {
799+ if f (obj ) {
800+ result = append (result , obj )
801+ }
802+ }
803+ return result
804+ }
805+
806+ // SelectByGvk will return the subset of objects that matches the provided GVK.
807+ func (o KubeObjects ) SelectByGvk (apiVersion , kind string ) KubeObjects {
808+ return o .Select (func (o * KubeObject ) bool {
809+ return o .IsGVK (apiVersion , kind )
810+ })
811+ }
812+
813+ // ExcludeByGvk will return the subset of objects that do not match the provided GVK.
814+ func (o KubeObjects ) ExcludeByGvk (apiVersion , kind string ) KubeObjects {
815+ return o .Select (func (o * KubeObject ) bool {
816+ return ! o .IsGVK (apiVersion , kind )
817+ })
818+ }
819+
820+ // SelectByName will return the subset of objects that matches the provided name.
821+ func (o KubeObjects ) SelectByName (name string ) KubeObjects {
822+ return o .Select (func (o * KubeObject ) bool {
823+ return o .GetName () == name
824+ })
825+ }
826+
827+ // ExcludeByName will return the subset of objects that do not match the provided name.
828+ func (o KubeObjects ) ExcludeByName (name string ) KubeObjects {
829+ return o .Select (func (o * KubeObject ) bool {
830+ return o .GetName () != name
831+ })
832+ }
833+
834+ // SelectByNamespace will return the subset of objects that matches the provided namespace.
835+ func (o KubeObjects ) SelectByNamespace (namespace string ) KubeObjects {
836+ return o .Select (func (o * KubeObject ) bool {
837+ return o .GetNamespace () == namespace
838+ })
839+ }
840+
841+ // ExcludeByNamespace will return the subset of objects that do not match the provided namespace.
842+ func (o KubeObjects ) ExcludeByNamespace (namespace string ) KubeObjects {
843+ return o .Select (func (o * KubeObject ) bool {
844+ return o .GetNamespace () != namespace
845+ })
846+ }
847+
848+ // SelectByLabels will return the subset of objects that matches the provided labels.
849+ func (o KubeObjects ) SelectByLabels (labels map [string ]string ) KubeObjects {
850+ return o .Select (func (o * KubeObject ) bool {
851+ return o .HasLabels (labels )
852+ })
853+ }
854+
855+ // ExcludeByLabels will return the subset of objects that do not match the provided labels.
856+ func (o KubeObjects ) ExcludeByLabels (labels map [string ]string ) KubeObjects {
857+ return o .Select (func (o * KubeObject ) bool {
858+ return ! o .HasLabels (labels )
859+ })
860+ }
861+
862+ // SelectByAnnotations will return the subset of objects that matches the provided labels.
863+ func (o KubeObjects ) SelectByAnnotations (annotations map [string ]string ) KubeObjects {
864+ return o .Select (func (o * KubeObject ) bool {
865+ return o .HasAnnotations (annotations )
866+ })
867+ }
868+
869+ // ExcludeByAnnotations will return the subset of objects that do not match the provided labels.
870+ func (o KubeObjects ) ExcludeByAnnotations (annotations map [string ]string ) KubeObjects {
871+ return o .Select (func (o * KubeObject ) bool {
872+ return ! o .HasAnnotations (annotations )
873+ })
874+ }
875+
876+ // SelectMetaResources will return the subset of objects that are meta resources. Currently, this
877+ // is just the Kptfile.
878+ func (o KubeObjects ) SelectMetaResources () KubeObjects {
879+ return o .SelectByGvk ("kpt.dev/v1" , "Kptfile" )
880+ }
881+
882+ // ExcludeMetaResources will return the subset of objects that are not meta resources. Currently, this
883+ // is just the Kptfile.
884+ func (o KubeObjects ) ExcludeMetaResources () KubeObjects {
885+ return o .ExcludeByGvk ("kpt.dev/v1" , "Kptfile" )
886+ }
887+
762888func (o * KubeObject ) IsEmpty () bool {
763889 return yaml .IsYNodeEmptyMap (o .obj .Node ())
764890}
0 commit comments