Skip to content

Commit

Permalink
Add KptFileGVK to expose typed GVK information for Kptfile
Browse files Browse the repository at this point in the history
More ergonomic than a set of strings.

Also do the same for ResultList and FunctionResultList
  • Loading branch information
justinsb committed Oct 14, 2022
1 parent 3eb9918 commit 15cc8cf
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 57 deletions.
2 changes: 1 addition & 1 deletion commands/live/migrate/migratecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (mr *Runner) retrieveConfigMapInv(reader io.Reader, args []string) (invento

// cli-utils treats any resource that contains the inventory-id label as an inventory object. We should
// ignore any inventories that are stored as ResourceGroup resources since they do not need migration.
if cmInvObj.GetKind() == rgfilev1alpha1.RGFileKind {
if cmInvObj.GetKind() == rgfilev1alpha1.ResourceGroupGVK().Kind {
// No ConfigMap inventory means the migration has already run before.
fmt.Fprintln(mr.ioStreams.Out, "no ConfigMap inventory...completed")
return nil, &inventory.NoInventoryObjError{}
Expand Down
10 changes: 6 additions & 4 deletions commands/pkg/get/cmdget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ func TestCmd_execute(t *testing.T) {
},
},
TypeMeta: yaml.TypeMeta{
APIVersion: kptfilev1.KptFileAPIVersion,
Kind: kptfilev1.KptFileKind},
APIVersion: kptfilev1.KptFileGVK().GroupVersion().String(),
Kind: kptfilev1.KptFileGVK().Kind,
},
},
Upstream: &kptfilev1.Upstream{
Type: kptfilev1.GitOrigin,
Expand Down Expand Up @@ -126,8 +127,9 @@ func TestCmdMainBranch_execute(t *testing.T) {
},
},
TypeMeta: yaml.TypeMeta{
APIVersion: kptfilev1.KptFileAPIVersion,
Kind: kptfilev1.KptFileKind},
APIVersion: kptfilev1.KptFileGVK().GroupVersion().String(),
Kind: kptfilev1.KptFileGVK().Kind,
},
},
Upstream: &kptfilev1.Upstream{
Type: kptfilev1.GitOrigin,
Expand Down
2 changes: 1 addition & 1 deletion internal/builtins/pkg_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const (

var (
configMapGVK = resid.NewGvk("", "v1", "ConfigMap")
kptfileGVK = resid.NewGvk(kptfilev1.KptFileGroup, kptfilev1.KptFileVersion, kptfilev1.KptFileKind)
kptfileGVK = resid.NewGvk(kptfilev1.KptFileGVK().Group, kptfilev1.KptFileGVK().Version, kptfilev1.KptFileGVK().Kind)
)

// PackageContextGenerator is a built-in KRM function that generates
Expand Down
40 changes: 23 additions & 17 deletions internal/pkg/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
rgfilev1alpha1 "github.com/GoogleContainerTools/kpt/pkg/api/resourcegroup/v1alpha1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/kubectl/pkg/util/slice"
"sigs.k8s.io/kustomize/kyaml/filesys"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
Expand All @@ -45,17 +44,17 @@ const (
pkgPathAnnotation = "internal.config.kubernetes.io/package-path"
)

var DeprecatedKptfileVersions = []string{
"v1alpha1",
"v1alpha2",
var DeprecatedKptfileVersions = []schema.GroupVersionKind{
kptfilev1.KptFileGVK().GroupKind().WithVersion("v1alpha1"),
kptfilev1.KptFileGVK().GroupKind().WithVersion("v1alpha2"),
}

// MatchAllKRM represents set of glob pattern to match all KRM
// resources including Kptfile.
var MatchAllKRM = append([]string{kptfilev1.KptFileName}, kio.MatchAll...)

var SupportedKptfileVersions = []string{
kptfilev1.KptFileVersion,
var SupportedKptfileVersions = []schema.GroupVersionKind{
kptfilev1.KptFileGVK(),
}

// KptfileError records errors regarding reading or parsing of a Kptfile.
Expand Down Expand Up @@ -286,18 +285,15 @@ func CheckKptfileVersion(content []byte) error {
if err != nil {
return err
}
gvk := gv.WithKind(kind)

switch {
// If the resource type matches what we are looking for, just return nil.
case gv.Group == kptfilev1.KptFileGroup &&
kind == kptfilev1.KptFileKind &&
isSupportedKptfileVersion(gv.Version):
case isSupportedKptfileVersion(gvk):
return nil
// If the kind and group is correct and the version is a known deprecated
// schema for the Kptfile, return DeprecatedKptfileError.
case gv.Group == kptfilev1.KptFileGroup &&
kind == kptfilev1.KptFileKind &&
isDeprecatedKptfileVersion(gv.Version):
case isDeprecatedKptfileVersion(gvk):
return &DeprecatedKptfileError{
Version: gv.Version,
}
Expand All @@ -310,12 +306,22 @@ func CheckKptfileVersion(content []byte) error {
}
}

func isDeprecatedKptfileVersion(version string) bool {
return slice.ContainsString(DeprecatedKptfileVersions, version, nil)
func isDeprecatedKptfileVersion(gvk schema.GroupVersionKind) bool {
for _, v := range DeprecatedKptfileVersions {
if v == gvk {
return true
}
}
return false
}

func isSupportedKptfileVersion(version string) bool {
return slice.ContainsString(SupportedKptfileVersions, version, nil)
func isSupportedKptfileVersion(gvk schema.GroupVersionKind) bool {
for _, v := range SupportedKptfileVersions {
if v == gvk {
return true
}
}
return false
}

// Pipeline returns the Pipeline section of the pkg's Kptfile.
Expand Down Expand Up @@ -777,7 +783,7 @@ func filterResourceGroups(input []*yaml.RNode) (output []*yaml.RNode, err error)
return nil, fmt.Errorf("failed to read metadata for resource %w", err)
}
// Filter out any non-ResourceGroup files.
if !(meta.APIVersion == rgfilev1alpha1.RGFileAPIVersion && meta.Kind == rgfilev1alpha1.RGFileKind) {
if !(meta.APIVersion == rgfilev1alpha1.ResourceGroupGVK().GroupVersion().String() && meta.Kind == rgfilev1alpha1.ResourceGroupGVK().Kind) {
continue
}

Expand Down
19 changes: 11 additions & 8 deletions internal/util/fetch/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ func TestCommand_Run(t *testing.T) {
},
},
TypeMeta: yaml.TypeMeta{
APIVersion: kptfilev1.KptFileAPIVersion,
Kind: kptfilev1.KptFileKind},
APIVersion: kptfilev1.KptFileGVK().GroupVersion().String(),
Kind: kptfilev1.KptFileGVK().Kind,
},
},
Upstream: &kptfilev1.Upstream{
Type: "git",
Expand Down Expand Up @@ -258,8 +259,8 @@ func TestCommand_Run_subdir(t *testing.T) {
},
},
TypeMeta: yaml.TypeMeta{
APIVersion: kptfilev1.KptFileAPIVersion,
Kind: kptfilev1.KptFileKind},
APIVersion: kptfilev1.KptFileGVK().GroupVersion().String(),
Kind: kptfilev1.KptFileGVK().Kind},
},
Upstream: &kptfilev1.Upstream{
Type: kptfilev1.GitOrigin,
Expand Down Expand Up @@ -334,8 +335,9 @@ func TestCommand_Run_branch(t *testing.T) {
},
},
TypeMeta: yaml.TypeMeta{
APIVersion: kptfilev1.KptFileAPIVersion,
Kind: kptfilev1.KptFileKind},
APIVersion: kptfilev1.KptFileGVK().GroupVersion().String(),
Kind: kptfilev1.KptFileGVK().Kind,
},
},
Upstream: &kptfilev1.Upstream{
Type: kptfilev1.GitOrigin,
Expand Down Expand Up @@ -415,8 +417,9 @@ func TestCommand_Run_tag(t *testing.T) {
},
},
TypeMeta: yaml.TypeMeta{
APIVersion: kptfilev1.KptFileAPIVersion,
Kind: kptfilev1.KptFileKind},
APIVersion: kptfilev1.KptFileGVK().GroupVersion().String(),
Kind: kptfilev1.KptFileGVK().Kind,
},
},
Upstream: &kptfilev1.Upstream{
Type: "git",
Expand Down
21 changes: 17 additions & 4 deletions pkg/api/fnresult/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package v1

import (
kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/kustomize/kyaml/fn/framework"
"sigs.k8s.io/kustomize/kyaml/yaml"
)
Expand All @@ -42,12 +42,25 @@ type Result struct {
}

const (
ResultListKind = "FunctionResultList"
ResultListGroup = kptfilev1.KptFileGroup
ResultListVersion = kptfilev1.KptFileVersion
// Deprecated: prefer ResultListGVK
ResultListKind = "FunctionResultList"
// Deprecated: prefer ResultListGVK
ResultListGroup = "kpt.dev"
// Deprecated: prefer ResultListGVK
ResultListVersion = "v1"
// Deprecated: prefer ResultListGVK
ResultListAPIVersion = ResultListGroup + "/" + ResultListVersion
)

// KptFileGVK is the GroupVersionKind of FunctionResultList objects
func ResultListGVK() schema.GroupVersionKind {
return schema.GroupVersionKind{
Group: "kpt.dev",
Version: "v1",
Kind: "FunctionResultList",
}
}

// ResultList contains aggregated results from multiple functions
type ResultList struct {
yaml.ResourceMeta `yaml:",inline"`
Expand Down
26 changes: 22 additions & 4 deletions pkg/api/kptfile/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,35 @@ package v1
import (
"fmt"

"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

const (
KptFileName = "Kptfile"
KptFileKind = "Kptfile"
KptFileGroup = "kpt.dev"
KptFileVersion = "v1"
KptFileName = "Kptfile"

// Deprecated: prefer KptFileGVK
KptFileKind = "Kptfile"

// Deprecated: prefer KptFileGVK
KptFileGroup = "kpt.dev"

// Deprecated: prefer KptFileGVK
KptFileVersion = "v1"

// Deprecated: prefer KptFileGVK
KptFileAPIVersion = KptFileGroup + "/" + KptFileVersion
)

// KptFileGVK is the GroupVersionKind of Kptfile objects
func KptFileGVK() schema.GroupVersionKind {
return schema.GroupVersionKind{
Group: "kpt.dev",
Version: "v1",
Kind: "Kptfile",
}
}

// TypeMeta is the TypeMeta for KptFile instances.
var TypeMeta = yaml.ResourceMeta{
TypeMeta: yaml.TypeMeta{
Expand Down
25 changes: 20 additions & 5 deletions pkg/api/resourcegroup/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,35 @@
package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

const (
RGFileName = "resourcegroup.yaml"
RGFileKind = "ResourceGroup"
RGFileGroup = "kpt.dev"
RGFileVersion = "v1alpha1"
RGFileAPIVersion = RGFileGroup + "/" + RGFileVersion
RGFileName = "resourcegroup.yaml"
// RGInventoryIDLabel is the label name used for storing an inventory ID.
RGInventoryIDLabel = common.InventoryLabel

// Deprecated: prefer ResourceGroupGVK
RGFileKind = "ResourceGroup"
// Deprecated: prefer ResourceGroupGVK
RGFileGroup = "kpt.dev"
// Deprecated: prefer ResourceGroupGVK
RGFileVersion = "v1alpha1"
// Deprecated: prefer ResourceGroupGVK
RGFileAPIVersion = RGFileGroup + "/" + RGFileVersion
)

// ResourceGroupGVK is the GroupVersionKind of ResourceGroup objects
func ResourceGroupGVK() schema.GroupVersionKind {
return schema.GroupVersionKind{
Group: "kpt.dev",
Version: "v1alpha1",
Kind: "ResourceGroup",
}
}

// DefaultMeta is the ResourceMeta for ResourceGroup instances.
var DefaultMeta = yaml.ResourceMeta{
TypeMeta: yaml.TypeMeta{
Expand Down
18 changes: 14 additions & 4 deletions pkg/live/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
rgfilev1alpha1 "github.com/GoogleContainerTools/kpt/pkg/api/resourcegroup/v1alpha1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/klog/v2"
"k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/pkg/common"
"sigs.k8s.io/cli-utils/pkg/inventory"
Expand Down Expand Up @@ -195,8 +197,8 @@ type InventoryFilter struct {
}

func (i *InventoryFilter) Filter(object *yaml.RNode) (*yaml.RNode, error) {
if object.GetApiVersion() != kptfilev1.KptFileAPIVersion ||
object.GetKind() != kptfilev1.KptFileKind {
if object.GetApiVersion() != kptfilev1.KptFileGVK().GroupVersion().String() ||
object.GetKind() != kptfilev1.KptFileGVK().Kind {
return object, nil
}

Expand All @@ -220,9 +222,17 @@ type RGFilter struct {
Inventories []*rgfilev1alpha1.ResourceGroup
}

func GroupVersionKindForObject(object *yaml.RNode) schema.GroupVersionKind {
apiVersion := object.GetApiVersion()
gv, err := schema.ParseGroupVersion(apiVersion)
if err != nil {
klog.Warningf("error parsing apiVersion=%q", apiVersion)
}
return gv.WithKind(object.GetKind())
}

func (r *RGFilter) Filter(object *yaml.RNode) (*yaml.RNode, error) {
if object.GetApiVersion() != rgfilev1alpha1.RGFileAPIVersion ||
object.GetKind() != rgfilev1alpha1.RGFileKind {
if GroupVersionKindForObject(object) != rgfilev1alpha1.ResourceGroupGVK() {
return object, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/live/rgpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (r *ResourceGroupPathManifestReader) Read() ([]*unstructured.Unstructured,
// Skip if current file is a ResourceGroup resource. We do not want to apply/delete any ResourceGroup CRs when we
// run any `kpt live` commands on a package. Instead, we have specific logic in place for handling ResourceGroups in
// the live cluster.
if u.GetKind() == rgfilev1alpha1.RGFileKind && u.GetAPIVersion() == rgfilev1alpha1.DefaultMeta.APIVersion {
if u.GroupVersionKind() == rgfilev1alpha1.ResourceGroupGVK() {
continue
}
objs = append(objs, u)
Expand Down
10 changes: 2 additions & 8 deletions pkg/live/rgstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,8 @@ import (

var (
excludedGKs = []schema.GroupKind{
{
Group: kptfilev1.KptFileGroup,
Kind: kptfilev1.KptFileKind,
},
{
Group: rgfilev1alpha1.RGFileGroup,
Kind: rgfilev1alpha1.RGFileKind,
},
kptfilev1.KptFileGVK().GroupKind(),
rgfilev1alpha1.ResourceGroupGVK().GroupKind(),
}
)

Expand Down

0 comments on commit 15cc8cf

Please sign in to comment.