Skip to content

Commit

Permalink
kubeobject lib: Remove getters and ToStruct
Browse files Browse the repository at this point in the history
  • Loading branch information
kispaljr committed Apr 28, 2023
1 parent 4f8a7b4 commit edca649
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 30 deletions.
25 changes: 0 additions & 25 deletions krm-functions/lib/kubeobject/kubeobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,6 @@ func (o *KubeObjectExt[T1]) SetStatus(newStatus interface{}) error {
// and thus nill checking of `obj` was omitted intentionally:
// the caller is responsible for ensuring that `obj` is not nil`

// ToStruct converts the KubeObject to a go struct with type `T`
func ToStruct[T any](obj *fn.KubeObject) (T, error) {
var x T
err := obj.As(&x)
return x, err
}

// GetSpec returns with the `spec` field of the KubeObject as a go struct with type `T`
// NOTE: consider using ToStruct() instead
func GetSpec[T any](obj *fn.KubeObject) (T, error) {
var spec T
err := obj.UpsertMap("spec").As(&spec)
return spec, err

}

// GetStatus returns with the `status` field of the KubeObject as a go struct with type `T`
// NOTE: consider using ToStruct() instead
func GetStatus[T any](obj *fn.KubeObject) (T, error) {
var status T
err := obj.UpsertMap("status").As(&status)
return status, err

}

// setNestedFieldKeepFormatting is similar to KubeObject.SetNestedField(), but keeps the
// comments and the order of fields in the YAML wherever it is possible.
//
Expand Down
10 changes: 5 additions & 5 deletions krm-functions/lib/kubeobject/kubeobject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func TestNewFromGoStruct(t *testing.T) {
}

// test with nil input
_, err := NewFromGoStruct[v1.Deployment](nil)
_, err := NewFromGoStruct[appsv1.Deployment](nil)
if err == nil {
t.Errorf("NewFromGoStruct(nil) doesn't return with an error")
}
Expand Down Expand Up @@ -366,15 +366,15 @@ func TestSetNestedFieldKeepFormatting(t *testing.T) {
t.Run(inputFile, func(t *testing.T) {
obj := testlib.MustParseKubeObject(t, inputFile)

deploy, err := ToStruct[appsv1.Deployment](obj)
if err != nil {
t.Errorf("unexpected error in ToStruct[v1.Deployment]: %v", err)
var deploy appsv1.Deployment
if err := obj.As(&deploy); err != nil {
t.Fatalf("couldn't convert object to Deployment: %v", err)
}

deploy.Spec.Replicas = nil // delete Replicas field if present
deploy.Spec.Template.Spec.RestartPolicy = corev1.RestartPolicyOnFailure // update field value

err = setNestedFieldKeepFormatting(&obj.SubObject, deploy.Spec, "spec")
err := setNestedFieldKeepFormatting(&obj.SubObject, deploy.Spec, "spec")
if err != nil {
t.Errorf("unexpected error in SetNestedFieldKeepFormatting: %v", err)
}
Expand Down

0 comments on commit edca649

Please sign in to comment.