Skip to content

Commit

Permalink
Merge pull request openshift#1617 from csrwng/remove-status
Browse files Browse the repository at this point in the history
Remove .status from serialized configuration on HCP
  • Loading branch information
openshift-merge-robot authored Jul 27, 2022
2 parents de891a3 + 609d61d commit 579d83b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
kjson "k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -4775,6 +4776,20 @@ func configurationFieldsToRawExtensions(config *hyperv1.ClusterConfiguration) ([
if err := serializer.Encode(result[idx].Object, b); err != nil {
return nil, fmt.Errorf("failed to marshal %+v: %w", result[idx].Object, err)
}

// Remove the status part of the serialized resource. We only have
// spec to begin with and status causes incompatibilities with previous
// versions of the CPO
unstructuredObject := &unstructured.Unstructured{}
if _, _, err := unstructured.UnstructuredJSONScheme.Decode(b.Bytes(), nil, unstructuredObject); err != nil {
return nil, fmt.Errorf("failed to decode resource into unstructured: %w", err)
}
unstructured.RemoveNestedField(unstructuredObject.Object, "status")
b = &bytes.Buffer{}
if err := unstructured.UnstructuredJSONScheme.Encode(unstructuredObject, b); err != nil {
return nil, fmt.Errorf("failed to serialize unstructured resource: %w", err)
}

result[idx].Raw = bytes.TrimSuffix(b.Bytes(), []byte("\n"))
result[idx].Object = nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"k8s.io/apimachinery/pkg/api/equality"
errors2 "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
Expand Down Expand Up @@ -1692,12 +1693,31 @@ func TestDefaultClusterIDsIfNeeded(t *testing.T) {
}

func TestConfigurationFieldsToRawExtensions(t *testing.T) {
config := &hyperv1.ClusterConfiguration{Proxy: &configv1.ProxySpec{HTTPProxy: "http://10.0.136.57:3128", HTTPSProxy: "http://10.0.136.57:3128"}}
config := &hyperv1.ClusterConfiguration{
Ingress: &configv1.IngressSpec{Domain: "example.com"},
Proxy: &configv1.ProxySpec{HTTPProxy: "http://10.0.136.57:3128", HTTPSProxy: "http://10.0.136.57:3128"},
}
result, err := configurationFieldsToRawExtensions(config)
if err != nil {
t.Fatalf("configurationFieldsToRawExtensions: %v", err)
}

// Check that serialized resources do not contain a status section
for i, rawExt := range result {
unstructuredObj := &unstructured.Unstructured{}
_, _, err := unstructured.UnstructuredJSONScheme.Decode(rawExt.Raw, nil, unstructuredObj)
if err != nil {
t.Fatalf("unexpected decode error: %v", err)
}
_, exists, err := unstructured.NestedFieldNoCopy(unstructuredObj.Object, "status")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if exists {
t.Errorf("status field exists for resource %d", i)
}
}

serialized, err := json.Marshal(result)
if err != nil {
t.Fatalf("json.Marshal: %v", err)
Expand All @@ -1714,13 +1734,28 @@ func TestConfigurationFieldsToRawExtensions(t *testing.T) {
t.Errorf("output does not match a json-roundtripped version: %s", diff)
}

var ingress configv1.Ingress
if err := json.Unmarshal(result[0].Raw, &ingress); err != nil {
t.Fatalf("failed to unmarshal raw data: %v", err)
}
if ingress.APIVersion == "" || ingress.Kind == "" {
t.Errorf("rawObject has no apiVersion or kind set: %+v", ingress.ObjectMeta)
}
if ingress.Spec.Domain != "example.com" {
t.Errorf("ingress does not have expected domain: %q", ingress.Spec.Domain)
}

var proxy configv1.Proxy
if err := json.Unmarshal(result[0].Raw, &proxy); err != nil {
if err := json.Unmarshal(result[1].Raw, &proxy); err != nil {
t.Fatalf("failed to unmarshal raw data: %v", err)
}
if proxy.APIVersion == "" || proxy.Kind == "" {
t.Errorf("rawObject has no apiVersion or kind set: %+v", proxy.ObjectMeta)
}
if proxy.Spec.HTTPProxy != "http://10.0.136.57:3128" {
t.Errorf("proxy does not have expected HTTPProxy: %q", proxy.Spec.HTTPProxy)
}

}

func TestIsUpgradeable(t *testing.T) {
Expand Down

0 comments on commit 579d83b

Please sign in to comment.