Skip to content

Commit

Permalink
Unexport 'status' field in k8s.Resource
Browse files Browse the repository at this point in the history
  • Loading branch information
maqiuyujoyce committed Mar 26, 2024
1 parent a72e4b9 commit 9956a8a
Show file tree
Hide file tree
Showing 27 changed files with 2,339 additions and 2,247 deletions.
4 changes: 3 additions & 1 deletion pkg/cli/gcpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ func updateResourceAndNewUnstructuredFromState(resource *krmtotf.Resource, state
resource.Name = krmtotf.GetNameFromState(resource, state)
resource.Labels = krmtotf.GetLabelsFromState(resource, state)
resource.Annotations = krmtotf.GetAnnotationsFromState(resource, state)
resource.Spec, resource.Status = krmtotf.ResolveSpecAndStatusWithResourceID(resource, state)
spec, status := krmtotf.ResolveSpecAndStatusWithResourceID(resource, state)
resource.Spec = spec
resource.SetStatus(status)
return resource.MarshalAsUnstructured()
}

Expand Down
11 changes: 10 additions & 1 deletion pkg/controller/dcl/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,8 @@ func (r *Reconciler) updateSpecAndStatusWithLiveState(ctx context.Context, liveL
if err != nil {
return false, r.HandleUpdateFailed(ctx, &resource.Resource, fmt.Errorf("error resolving the live state: %w", err))
}
resource.Spec, resource.Status = newSpec, newStatus
resource.Spec = newSpec
resource.SetStatus(newStatus)

if err := updateMutableButUnreadableFieldsAnnotationFor(resource); err != nil {
return false, err
Expand Down Expand Up @@ -532,6 +533,14 @@ func (r *Reconciler) constructDesiredStateWithManagedFields(original *dcl.Resour
if err := util.Marshal(u, res); err != nil {
return nil, err
}
statusObj := u.Object["status"]
if statusObj != nil {
statusObjInMap, ok := statusObj.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("expected status value to be map[string]interface{} GroupVersionKind %v but was actually %T", gvk, statusObj)
}
res.SetStatus(statusObjInMap)
}
if val, ok := original.Spec[k8s.ResourceIDFieldName]; ok {
if res.Spec == nil {
res.Spec = make(map[string]interface{})
Expand Down
8 changes: 8 additions & 0 deletions pkg/controller/direct/directbase/directbase_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,5 +414,13 @@ func toK8sResource(policy *unstructured.Unstructured) (*k8s.Resource, error) {
if err := util.Marshal(policy, &resource); err != nil {
return nil, fmt.Errorf("error marshalling to k8s resource: %w", err)
}
statusObj := policy.Object["status"]
if statusObj != nil {
statusObjInMap, ok := statusObj.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("expected status value to be map[string]interface{} but was actually %T", statusObj)
}
resource.SetStatus(statusObjInMap)
}
return &resource, nil
}
5 changes: 5 additions & 0 deletions pkg/controller/iam/auditconfig/iamauditconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,5 +393,10 @@ func ToK8sResource(auditConfig *iamv1beta1.IAMAuditConfig) (*k8s.Resource, error
if err := util.Marshal(auditConfig, &resource); err != nil {
return nil, fmt.Errorf("error marshalling IAMAuditConfig to k8s resource: %w", err)
}
statusObjInMap := make(map[string]interface{})
if err := util.Marshal(auditConfig.Status, &statusObjInMap); err != nil {
return nil, fmt.Errorf("error marshalling IAMAuditConfig.Status to map[string]interface{}: %w", err)
}
resource.SetStatus(statusObjInMap)
return &resource, nil
}
12 changes: 9 additions & 3 deletions pkg/controller/iam/iamclient/tfiamclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,9 @@ func resolveTargetFieldValue(r *krmtotf.Resource, targetField string) (string, e
}

func newIAMPolicyFromTFState(resource *krmtotf.Resource, state *terraform.InstanceState, origPolicy *v1beta1.IAMPolicy) (*v1beta1.IAMPolicy, error) {
resource.Spec, resource.Status = krmtotf.GetSpecAndStatusFromState(resource, state)
spec, status := krmtotf.GetSpecAndStatusFromState(resource, state)
resource.Spec = spec
resource.SetStatus(status)
if err := embedPolicyData(resource.Spec); err != nil {
return nil, err
}
Expand All @@ -716,7 +718,9 @@ func newIAMPolicyFromTFState(resource *krmtotf.Resource, state *terraform.Instan
}

func newIAMPolicyMemberFromTFState(resource *krmtotf.Resource, state *terraform.InstanceState, origPolicyMember *v1beta1.IAMPolicyMember) (*v1beta1.IAMPolicyMember, error) {
resource.Spec, resource.Status = krmtotf.GetSpecAndStatusFromState(resource, state)
spec, status := krmtotf.GetSpecAndStatusFromState(resource, state)
resource.Spec = spec
resource.SetStatus(status)
u, err := resource.MarshalAsUnstructured()
if err != nil {
return nil, fmt.Errorf("error marshalling resource to unstructured: %w", err)
Expand All @@ -735,7 +739,9 @@ func newIAMPolicyMemberFromTFState(resource *krmtotf.Resource, state *terraform.
}

func newIAMAuditConfigFromTFState(resource *krmtotf.Resource, state *terraform.InstanceState, origAuditConfig *v1beta1.IAMAuditConfig) (*v1beta1.IAMAuditConfig, error) {
resource.Spec, resource.Status = krmtotf.GetSpecAndStatusFromState(resource, state)
spec, status := krmtotf.GetSpecAndStatusFromState(resource, state)
resource.Spec = spec
resource.SetStatus(status)
if auditLogConfigs, ok := resource.Spec["auditLogConfig"]; ok {
resource.Spec["auditLogConfigs"] = auditLogConfigs
delete(resource.Spec, "auditLogConfig")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,11 @@ func toK8sResource(policy *iamv1beta1.IAMPartialPolicy) (*k8s.Resource, error) {
if err := util.Marshal(policy, &resource); err != nil {
return nil, fmt.Errorf("error marshalling IAMPartialPolicy to k8s resource: %w", err)
}
statusObjInMap := make(map[string]interface{})
if err := util.Marshal(policy.Status, &statusObjInMap); err != nil {
return nil, fmt.Errorf("error marshalling IAMPartialPolicy.Status to map[string]interface{}: %w", err)
}
resource.SetStatus(statusObjInMap)
return &resource, nil
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/controller/iam/policy/iampolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,5 +393,10 @@ func toK8sResource(policy *iamv1beta1.IAMPolicy) (*k8s.Resource, error) {
if err := util.Marshal(policy, &resource); err != nil {
return nil, fmt.Errorf("error marshalling IAMPolicy to k8s resource: %w", err)
}
statusObjInMap := make(map[string]interface{})
if err := util.Marshal(policy.Status, &statusObjInMap); err != nil {
return nil, fmt.Errorf("error marshalling IAMPolicy.Status to map[string]interface{}: %w", err)
}
resource.SetStatus(statusObjInMap)
return &resource, nil
}
5 changes: 5 additions & 0 deletions pkg/controller/iam/policymember/iampolicymember_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,5 +406,10 @@ func ToK8sResource(policyMember *iamv1beta1.IAMPolicyMember) (*k8s.Resource, err
if err := util.Marshal(policyMember, &resource); err != nil {
return nil, fmt.Errorf("error marshalling IAMPolicyMember to k8s resource: %w", err)
}
statusObjInMap := make(map[string]interface{})
if err := util.Marshal(policyMember.Status, &statusObjInMap); err != nil {
return nil, fmt.Errorf("error marshalling IAMPolicyMember.Status to map[string]interface{}: %w", err)
}
resource.SetStatus(statusObjInMap)
return &resource, nil
}
16 changes: 8 additions & 8 deletions pkg/controller/lifecyclehandler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (r *LifecycleHandler) updateStatus(ctx context.Context, resource *k8s.Resou
func (r *LifecycleHandler) updateAPIServer(ctx context.Context, resource *k8s.Resource) error {
// Preserve the intended status, as the client.Update call will ignore the given status
// and return the stale existing status.
status := deepcopy.MapStringInterface(resource.Status)
status := deepcopy.MapStringInterface(resource.GetStatus())
// Get the current generation as the observed generation because the following client.Update
// might increase the generation. We want the next reconciliation to handle the new generation.
observedGeneration := resource.GetGeneration()
Expand Down Expand Up @@ -114,7 +114,7 @@ func (r *LifecycleHandler) updateAPIServer(ctx context.Context, resource *k8s.Re
// Status updates for successful deletions must be handled independently.
return nil
}
resource.Status = status
resource.SetStatus(status)
setObservedGeneration(resource, observedGeneration)
return r.updateStatus(ctx, resource)
}
Expand Down Expand Up @@ -367,8 +367,8 @@ func (r *LifecycleHandler) HandleUnmanaged(ctx context.Context, resource *k8s.Re
}

func setCondition(resource *k8s.Resource, status corev1.ConditionStatus, reason, msg string) {
if resource.Status == nil {
resource.Status = make(map[string]interface{})
if resource.GetStatus() == nil {
resource.SetStatus(make(map[string]interface{}))
}
newReadyCondition := k8s.NewCustomReadyCondition(status, reason, msg)
// We should only update the ready condition's last transition time if there was a transition
Expand All @@ -379,14 +379,14 @@ func setCondition(resource *k8s.Resource, status corev1.ConditionStatus, reason,
newReadyCondition.LastTransitionTime = currentReadyCondition.LastTransitionTime
}
}
resource.Status["conditions"] = []k8sv1alpha1.Condition{newReadyCondition}
resource.GetStatus()["conditions"] = []k8sv1alpha1.Condition{newReadyCondition}
}

func setObservedGeneration(resource *k8s.Resource, observedGeneration int64) {
if resource.Status == nil {
resource.Status = make(map[string]interface{})
if resource.GetStatus() == nil {
resource.SetStatus(make(map[string]interface{}))
}
resource.Status["observedGeneration"] = observedGeneration
resource.GetStatus()["observedGeneration"] = observedGeneration
}

func (r *LifecycleHandler) recordEvent(ctx context.Context, resource *k8s.Resource, eventtype, reason, message string) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/tf/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,9 @@ func (r *Reconciler) handleDeleted(ctx context.Context, resource *krmtotf.Resour
}

func (r *Reconciler) handleUpToDate(ctx context.Context, resource *krmtotf.Resource, liveState *terraform.InstanceState, secretVersions map[string]string) error {
resource.Spec, resource.Status = krmtotf.ResolveSpecAndStatusWithResourceID(resource, liveState)
spec, status := krmtotf.ResolveSpecAndStatusWithResourceID(resource, liveState)
resource.Spec = spec
resource.SetStatus(status)
if err := updateMutableButUnreadableFieldsAnnotationFor(resource); err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/dcl/kcclite/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func resolveTargetFieldValue(refResource *k8s.Resource, typeConfig *corekccv1alp
}
return val, nil
}
if val, exist, _ := unstructured.NestedString(refResource.Status, strings.Split(typeConfig.TargetField, ".")...); exist {
if val, exist, _ := unstructured.NestedString(refResource.GetStatus(), strings.Split(typeConfig.TargetField, ".")...); exist {
return val, nil
}
if val, exist, _ := unstructured.NestedString(refResource.Spec, strings.Split(typeConfig.TargetField, ".")...); exist {
Expand Down Expand Up @@ -507,15 +507,15 @@ func resolveMixedSpecAndLegacyStatus(state *unstructured.Unstructured, resource
if !found {
status = make(map[string]interface{})
}
conditions, found, err := unstructured.NestedFieldCopy(resource.Status, "conditions")
conditions, found, err := unstructured.NestedFieldCopy(resource.GetStatus(), "conditions")
if err != nil {
return nil, nil, fmt.Errorf("error resolving conditions from resource status: %w", err)
}
if found {
status["conditions"] = conditions
}
// preserve the observedGeneration value
g, found, err := unstructured.NestedFieldCopy(resource.Status, "observedGeneration")
g, found, err := unstructured.NestedFieldCopy(resource.GetStatus(), "observedGeneration")
if err != nil {
return nil, nil, fmt.Errorf("error resolving observedGeneration from resource status: %w", err)
}
Expand Down
Loading

0 comments on commit 9956a8a

Please sign in to comment.