Skip to content

Commit

Permalink
chore: change transitiveResources type to TypeLocalObjectReference (#…
Browse files Browse the repository at this point in the history
…1211)

Signed-off-by: Jakob Steiner <jakob.steiner@glasskube.eu>
  • Loading branch information
kosmoz authored Sep 9, 2024
1 parent dbfd2a9 commit 54c78cd
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 51 deletions.
15 changes: 5 additions & 10 deletions api/v1alpha1/package_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"strings"

"github.com/invopop/jsonschema"
corev1 "k8s.io/api/core/v1"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type HelmManifest struct {
Expand Down Expand Up @@ -110,11 +110,6 @@ func (s *PackageScope) IsNamespaced() bool {
return s != nil && *s == ScopeNamespaced
}

type TransitiveResource struct {
metav1.GroupVersionKind `json:",inline"`
Name string `json:"name" jsonschema:"required"`
}

type PackageManifest struct {
// Scope is optional (default is Cluster)
Scope *PackageScope `json:"scope,omitempty"`
Expand All @@ -126,10 +121,10 @@ type PackageManifest struct {
// Helm instructs the controller to create a helm release when installing this package.
Helm *HelmManifest `json:"helm,omitempty"`
// Kustomize instructs the controller to apply a kustomization when installing this package [PLACEHOLDER].
Kustomize *KustomizeManifest `json:"kustomize,omitempty"`
Manifests []PlainManifest `json:"manifests,omitempty"`
ValueDefinitions map[string]ValueDefinition `json:"valueDefinitions,omitempty"`
TransitiveResources []TransitiveResource `json:"transitiveResources,omitempty"`
Kustomize *KustomizeManifest `json:"kustomize,omitempty"`
Manifests []PlainManifest `json:"manifests,omitempty"`
ValueDefinitions map[string]ValueDefinition `json:"valueDefinitions,omitempty"`
TransitiveResources []corev1.TypedLocalObjectReference `json:"transitiveResources,omitempty"`
// DefaultNamespace to install the package. May be overridden.
DefaultNamespace string `json:"defaultNamespace,omitempty" jsonschema:"required"`
Entrypoints []PackageEntrypoint `json:"entrypoints,omitempty"`
Expand Down
22 changes: 4 additions & 18 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions config/crd/bases/packages.glasskube.dev_packageinfos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -256,21 +256,27 @@ spec:
type: string
transitiveResources:
items:
description: |-
TypedLocalObjectReference contains enough information to let you locate the
typed referenced object inside the same namespace.
properties:
group:
apiGroup:
description: |-
APIGroup is the group for the resource being referenced.
If APIGroup is not specified, the specified Kind must be in the core API group.
For any other third-party types, APIGroup is required.
type: string
kind:
description: Kind is the type of resource being referenced
type: string
name:
type: string
version:
description: Name is the name of resource being referenced
type: string
required:
- group
- kind
- name
- version
type: object
x-kubernetes-map-type: atomic
type: array
valueDefinitions:
additionalProperties:
Expand Down
28 changes: 21 additions & 7 deletions internal/manifest/plain/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/glasskube/glasskube/api/v1alpha1"
"github.com/glasskube/glasskube/internal/controller/ctrlpkg"
"github.com/glasskube/glasskube/internal/kustomize"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -19,7 +20,10 @@ func prefixAndUpdateReferences(
) ([]client.Object, error) {
if pkg.IsNamespaceScoped() {
kustomization := createKustomization(pkg)
transitiveObjects := createTransitiveObjects(manifest.TransitiveResources)
transitiveObjects, err := createTransitiveObjects(manifest.TransitiveResources)
if err != nil {
return nil, err
}
if result, err := kustomize.KustomizeObjects(kustomization, append(objects, transitiveObjects...)); err != nil {
return nil, err
} else {
Expand Down Expand Up @@ -48,20 +52,30 @@ func createKustomization(pkg ctrlpkg.Package) kstypes.Kustomization {
}
}

func createTransitiveObjects(resList []v1alpha1.TransitiveResource) []client.Object {
func createTransitiveObjects(resList []v1.TypedLocalObjectReference) ([]client.Object, error) {
result := make([]client.Object, len(resList))
for i, res := range resList {
result[i] = createTransitiveObject(res)
if obj, err := createTransitiveObject(res); err != nil {
return nil, err
} else {
result[i] = obj
}
}
return result
return result, nil
}

// createTransitiveObject creates a "stub" [client.Object] for a given [v1alpha1.TransitiveResource].
// This Object can not be applied in a cluster and serve only as "reference", so the kustomize name reference
// transformer updates any references to them.
func createTransitiveObject(res v1alpha1.TransitiveResource) client.Object {
func createTransitiveObject(res v1.TypedLocalObjectReference) (client.Object, error) {
var result unstructured.Unstructured
if res.APIGroup == nil {
result.SetGroupVersionKind(schema.GroupVersionKind{Kind: res.Kind})
} else if gv, err := schema.ParseGroupVersion(*res.APIGroup); err != nil {
return nil, err
} else {
result.SetGroupVersionKind(gv.WithKind(res.Kind))
}
result.SetName(res.Name)
result.SetGroupVersionKind(schema.GroupVersionKind(res.GroupVersionKind))
return &result
return &result, nil
}
9 changes: 6 additions & 3 deletions internal/manifest/plain/prefix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"io"

"github.com/glasskube/glasskube/api/v1alpha1"
"github.com/glasskube/glasskube/internal/util"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/yaml"
Expand Down Expand Up @@ -199,10 +201,11 @@ var _ = Describe("updateReferences", func() {
objs := parseObjs(secretAndDeployment)
expectedObj := parseObjs(secretAndDeploymentExpectedWithTransitive)
newObjs, err := prefixAndUpdateReferences(pkg, &v1alpha1.PackageManifest{
TransitiveResources: []v1alpha1.TransitiveResource{
TransitiveResources: []corev1.TypedLocalObjectReference{
{
GroupVersionKind: metav1.GroupVersionKind{Version: "v1", Kind: "Secret"},
Name: "test1",
APIGroup: util.Pointer("v1"),
Kind: "Secret",
Name: "test1",
},
},
}, objs)
Expand Down
12 changes: 4 additions & 8 deletions website/static/schemas/v1/package-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,9 @@
"url"
]
},
"TransitiveResource": {
"TypedLocalObjectReference": {
"properties": {
"group": {
"type": "string"
},
"version": {
"apiGroup": {
"type": "string"
},
"kind": {
Expand All @@ -163,8 +160,7 @@
"additionalProperties": false,
"type": "object",
"required": [
"group",
"version",
"apiGroup",
"kind",
"name"
]
Expand Down Expand Up @@ -356,7 +352,7 @@
},
"transitiveResources": {
"items": {
"$ref": "#/$defs/TransitiveResource"
"$ref": "#/$defs/TypedLocalObjectReference"
},
"type": "array"
},
Expand Down

0 comments on commit 54c78cd

Please sign in to comment.