Skip to content

Commit

Permalink
Remove deprecated secretRef field from Target structure (#1243)
Browse files Browse the repository at this point in the history
* Test marshaling of targets

* Remove deprecated field (run-int-tests)
  • Loading branch information
robertgraeff authored Sep 19, 2024
1 parent 1ab3098 commit aadbb72
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 59 deletions.
25 changes: 2 additions & 23 deletions apis/core/v1alpha1/targettypes/kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,23 @@ const DefaultKubeconfigKey = "kubeconfig"
// ValueRef holds a value that can be either defined by string or by a secret ref.
type ValueRef struct {
StrVal *string `json:"-"`

// deprecated
SecretRef *v1alpha1.SecretReference `json:"secretRef,omitempty"`
}

// kubeconfigJSON is a helper struct for decoding.
type kubeconfigJSON struct {
Kubeconfig *ValueRef `json:"kubeconfig"`
}

// valueRefJSON is a helper struct to decode json into a secret ref object.
type valueRefJSON struct {
SecretRef *v1alpha1.SecretReference `json:"secretRef,omitempty"`
}

// MarshalJSON implements the json marshaling for a JSON
func (v ValueRef) MarshalJSON() ([]byte, error) {
if v.StrVal != nil {
return json.Marshal(v.StrVal)
}
ref := valueRefJSON{
SecretRef: v.SecretRef,
}
return json.Marshal(ref)
return json.Marshal(v.StrVal)
}

// UnmarshalJSON implements json unmarshaling for a JSON
func (v *ValueRef) UnmarshalJSON(data []byte) error {
ref := &valueRefJSON{}
err := json.Unmarshal(data, ref)
if err == nil && ref.SecretRef != nil {
// parsing into secret reference was successful
v.SecretRef = ref.SecretRef
return nil
}
// parse into string instead
var strVal string
err = json.Unmarshal(data, &strVal)
err := json.Unmarshal(data, &strVal)
if err == nil {
v.StrVal = &strVal
return nil
Expand Down
54 changes: 54 additions & 0 deletions apis/core/v1alpha1/types_target_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package v1alpha1_test

import (
"encoding/json"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/utils/ptr"

"github.com/gardener/landscaper/apis/core/v1alpha1"
"github.com/gardener/landscaper/apis/core/v1alpha1/targettypes"
)

var _ = Describe("Target", func() {

It("should marshal a target with inline kubeconfig", func() {
targetConfig := &targettypes.KubernetesClusterTargetConfig{
Kubeconfig: targettypes.ValueRef{
StrVal: ptr.To("a: 1\nb: 2"),
},
}
targetConfigJSON, err := json.Marshal(targetConfig)
Expect(err).NotTo(HaveOccurred())
Expect(targetConfigJSON).To(MatchJSON(`{"kubeconfig":"a: 1\nb: 2"}`))

target := &v1alpha1.Target{
Spec: v1alpha1.TargetSpec{
Type: targettypes.KubernetesClusterTargetType,
Configuration: &v1alpha1.AnyJSON{
RawMessage: targetConfigJSON,
},
},
}
targetJSON, err := json.Marshal(target)
Expect(err).NotTo(HaveOccurred())

Expect(targetJSON).To(MatchJSON(`{"metadata":{"creationTimestamp":null},"spec":{"type":"landscaper.gardener.cloud/kubernetes-cluster","config":{"kubeconfig":"a: 1\nb: 2"}}}`))
})

It("should unmarshal a target with inline kubeconfig", func() {
targetJSON := []byte(`{"metadata":{"creationTimestamp":null},"spec":{"type":"landscaper.gardener.cloud/kubernetes-cluster","config":{"kubeconfig":"a: 1\nb: 2"}}}`)
target := &v1alpha1.Target{}
Expect(json.Unmarshal(targetJSON, target)).To(Succeed())

configJSON := target.Spec.Configuration.RawMessage
config := &targettypes.KubernetesClusterTargetConfig{}
Expect(json.Unmarshal(configJSON, config)).To(Succeed())
Expect(config).To(Equal(&targettypes.KubernetesClusterTargetConfig{
Kubeconfig: targettypes.ValueRef{
StrVal: ptr.To("a: 1\nb: 2"),
},
}))
})
})
39 changes: 3 additions & 36 deletions pkg/deployer/lib/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -43,7 +42,7 @@ func GetRestConfigAndClientAndClientSet(ctx context.Context, resolvedTarget *lsv
return nil, nil, nil, fmt.Errorf("unable to parse target confíguration: %w", err)
}

kubeconfigBytes, err := GetKubeconfigFromTargetConfig(ctx, targetConfig, resolvedTarget.Namespace, lsUncachedClient)
kubeconfigBytes, err := GetKubeconfigFromTargetConfig(targetConfig)
if err != nil {
return nil, nil, nil, err
}
Expand Down Expand Up @@ -71,43 +70,11 @@ func GetRestConfigAndClientAndClientSet(ctx context.Context, resolvedTarget *lsv

// GetKubeconfigFromTargetConfig fetches the kubeconfig from a given config.
// If the config defines the target from a secret that secret is read from all provided clients.
func GetKubeconfigFromTargetConfig(ctx context.Context, config *targettypes.KubernetesClusterTargetConfig,
targetNamespace string, lsClient client.Client) ([]byte, error) {
func GetKubeconfigFromTargetConfig(config *targettypes.KubernetesClusterTargetConfig) ([]byte, error) {
if config.Kubeconfig.StrVal != nil {
return []byte(*config.Kubeconfig.StrVal), nil
}
if config.Kubeconfig.SecretRef == nil {
return nil, errors.New("kubeconfig not defined")
}

return GetKubeconfigFromSecretRef(ctx, config.Kubeconfig.SecretRef, targetNamespace, lsClient)
}

func GetKubeconfigFromSecretRef(ctx context.Context, ref *lsv1alpha1.SecretReference, targetNamespace string,
lsClient client.Client) ([]byte, error) {

if len(ref.Namespace) > 0 && ref.Namespace != targetNamespace {
return nil, fmt.Errorf("namespace of secret ref %s differs from target namespace %s",
ref.Namespace, targetNamespace)
}

secret := &corev1.Secret{}
secretKey := client.ObjectKey{Name: ref.Name, Namespace: targetNamespace}
if err := read_write_layer.GetSecret(ctx, lsClient, secretKey, secret, read_write_layer.R000050); err != nil {
return nil, apierrors.NewNotFound(schema.GroupResource{
Resource: "secret",
}, ref.Name)
}

if len(ref.Key) == 0 {
ref.Key = targettypes.DefaultKubeconfigKey
}

kubeconfig, ok := secret.Data[ref.Key]
if !ok {
return nil, fmt.Errorf("secret found but key %q not found", ref.Key)
}
return kubeconfig, nil
return nil, errors.New("kubeconfig not defined")
}

// CreateOrUpdateExport creates or updates the export of a deploy item.
Expand Down

0 comments on commit aadbb72

Please sign in to comment.