import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/go-containerregistry/pkg/name" "github.com/google/go-containerregistry/pkg/v1" "github.com/google/go-containerregistry/pkg/v1/random" "github.com/google/go-containerregistry/pkg/v1/validate" "github.com/google/go-containerregistry/pkg/v1/whitelist" ) // Validate the image only runs on the architecture specified. func ExampleFilter() { // Set up a test image. img, err := random.Image(1024, 1) if err != nil { log.Fatal(err) } // Validate the manifest architecture. name, err := name.ParseReference("example.com/example") if err != nil { log.Fatal(err) } v1.Validator = whitelist.DefaultPolicy(nil).Validator() manifest, _, err := img.Manifest() if err != nil { log.Fatal(err) } if err := v1.Validator.Validate(manifest); err != nil { log.Fatal(err) } // Select a matching node. candidate := &v1.Node{ Kind: "Node", Spec: v1.NodeSpec{ Architecture: "amd64", }, } choose := func(n v1.Node) bool { ok, _ := fields.SelectorMatches(candidate.Spec.Architecture).Matches(&n) return ok } // Assert that the selected node matches. nodes, err := img.Nodes() if err != nil { log.Fatal(err) } if got := nodes.Filter(choose); len(got) != 1 { log.Fatalf("Filter(%v) returned:\n%v", cmpopts.EquateEmpty(), got) } }
import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/google/go-containerregistry/pkg/name" "github.com/google/go-containerregistry/pkg/v1" "github.com/google/go-containerregistry/pkg/v1/random" "github.com/google/go-containerregistry/pkg/v1/validate" "github.com/google/go-containerregistry/pkg/v1/whitelist" ) // Validate the image only runs on the architecture specified. func ExampleFilter() { // Set up a test image. img, err := random.Image(1024, 1) if err != nil { log.Fatal(err) } // Validate the manifest architecture. name, err := name.ParseReference("example.com/example") if err != nil { log.Fatal(err) } v1.Validator = whitelist.DefaultPolicy(nil).Validator() manifest, _, err := img.Manifest() if err != nil { log.Fatal(err) } if err := v1.Validator.Validate(manifest); err != nil { log.Fatal(err) } // Select a matching node. candidate := &v1.Node{ Kind: "Node", Spec: v1.NodeSpec{ Architecture: "amd64", }, } choose := func(n v1.Node) bool { ok, _ := fields.SelectorMatches(candidate.Spec.Architecture).Matches(&n) return ok } // Assert that the selected node matches. nodes, err := img.Nodes() if err != nil { log.Fatal(err) } if got := nodes.Filter(choose); len(got) != 1 { log.Fatalf("Filter(%v) returned:\n%v", cmpopts.EquateEmpty(), got) } }In this example, selectors are used to filter Kubernetes objects based on specific labels. The `SelectMatches` function is used to filter only pods whose labels include "app=myapp". Based on the import path, it appears that this package is specifically tailored for working with Kubernetes objects in the context of the Google Cloud Platform.