-
Notifications
You must be signed in to change notification settings - Fork 83
Refactors the kstatus functions and adds unstructured support in the version check #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
k8s-ci-robot
merged 4 commits into
kubernetes-sigs:master
from
somtochiama:helper-functions
Aug 16, 2020
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,42 +74,32 @@ func (a *aggregator) Reconciled(ctx context.Context, src declarative.Declarative | |
|
||
unstruct, ok := src.(*unstructured.Unstructured) | ||
instance, commonOkay := src.(addonv1alpha1.CommonObject) | ||
|
||
unstructStatus := make(map[string]interface{}) | ||
var status addonv1alpha1.CommonStatus | ||
|
||
if ok { | ||
unstructStatus["Healthy"] = true | ||
} else if commonOkay { | ||
status = addonv1alpha1.CommonStatus{Healthy: true} | ||
} else { | ||
return fmt.Errorf("object %T was not an addonv1alpha1.CommonObject", src) | ||
} | ||
changed := false | ||
|
||
if commonOkay { | ||
var status = instance.GetCommonStatus() | ||
status.Errors = statusErrors | ||
status.Healthy = statusHealthy | ||
|
||
if !reflect.DeepEqual(status, instance.GetCommonStatus()) { | ||
instance.SetCommonStatus(status) | ||
status.Healthy = statusHealthy | ||
|
||
log.WithValues("name", instance.GetName()).WithValues("status", status).Info("updating status") | ||
|
||
err := a.client.Status().Update(ctx, instance) | ||
if err != nil { | ||
log.Error(err, "updating status") | ||
return err | ||
} | ||
changed = true | ||
} | ||
} else { | ||
unstructStatus["Healthy"] = true | ||
unstructStatus["Errors"] = statusErrors | ||
} else if ok { | ||
unstructStatus := make(map[string]interface{}) | ||
|
||
s, _, err := unstructured.NestedMap(unstruct.Object, "status") | ||
if err != nil { | ||
log.Error(err, "getting status") | ||
return fmt.Errorf("unable to get status from unstructured: %v", err) | ||
} | ||
if !reflect.DeepEqual(status, s) { | ||
|
||
unstructStatus = s | ||
unstructStatus["Healthy"] = statusHealthy | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it |
||
unstructStatus["Errors"] = statusErrors | ||
if !reflect.DeepEqual(unstruct, s) { | ||
err = unstructured.SetNestedField(unstruct.Object, statusHealthy, "status", "healthy") | ||
if err != nil { | ||
log.Error(err, "updating status") | ||
|
@@ -121,22 +111,26 @@ func (a *aggregator) Reconciled(ctx context.Context, src declarative.Declarative | |
log.Error(err, "updating status") | ||
return fmt.Errorf("unable to set status in unstructured: %v", err) | ||
} | ||
changed = true | ||
} | ||
} else { | ||
return fmt.Errorf("instance %T was not an addonsv1alpha1.CommonObject or unstructured.Unstructured", src) | ||
} | ||
|
||
log.WithValues("name", unstruct.GetName()).WithValues("status", status).Info("updating status") | ||
|
||
err = a.client.Status().Update(ctx, unstruct) | ||
if err != nil { | ||
log.Error(err, "updating status") | ||
return err | ||
} | ||
if changed == true { | ||
log.WithValues("name", src.GetName()).WithValues("status", statusHealthy).Info("updating status") | ||
err := a.client.Status().Update(ctx, src) | ||
if err != nil { | ||
log.Error(err, "updating status") | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (a *aggregator) deployment(ctx context.Context, src declarative.DeclarativeObject, name string) (bool, error) { | ||
key := client.ObjectKey{Namespace: src.GetNamespace(), Name: name} | ||
key := client.ObjectKey{Name: name} | ||
dep := &appsv1.Deployment{} | ||
|
||
if err := a.client.Get(ctx, key, dep); err != nil { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package status | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"sigs.k8s.io/cli-utils/pkg/kstatus/status" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
addonsv1alpha1 "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/apis/v1alpha1" | ||
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative" | ||
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/manifest" | ||
) | ||
|
||
type kstatusAggregator struct { | ||
client client.Client | ||
reconciler *declarative.Reconciler | ||
} | ||
|
||
func NewKstatusAgregator(c client.Client, reconciler *declarative.Reconciler) *kstatusAggregator { | ||
return &kstatusAggregator{client: c, reconciler: reconciler} | ||
} | ||
|
||
func (k *kstatusAggregator) Reconciled(ctx context.Context, src declarative.DeclarativeObject, | ||
objs *manifest.Objects) error { | ||
log := log.Log | ||
|
||
statusMap := make(map[status.Status]bool) | ||
for _, object := range objs.Items { | ||
|
||
unstruct, err := declarative.GetObjectFromCluster(object, k.reconciler) | ||
if err != nil { | ||
log.WithValues("object", object.Kind+"/"+object.Name).Error(err, "Unable to get status of object") | ||
return err | ||
} | ||
|
||
res, err := status.Compute(unstruct) | ||
if err != nil { | ||
log.WithValues("kind", object.Kind).WithValues("name", object.Name).WithValues("status", res.Status).WithValues( | ||
"message", res.Message).Info("Got status of resource:") | ||
statusMap[status.NotFoundStatus] = true | ||
} | ||
if res != nil { | ||
log.WithValues("kind", object.Kind).WithValues("name", object.Name).WithValues("status", res.Status).WithValues("message", res.Message).Info("Got status of resource:") | ||
statusMap[res.Status] = true | ||
} | ||
} | ||
|
||
addonObject, ok := src.(addonsv1alpha1.CommonObject) | ||
unstruct, unstructOk := src.(*unstructured.Unstructured) | ||
aggregatedPhase := string(aggregateStatus(statusMap)) | ||
changed := false | ||
if ok { | ||
addonStatus := addonObject.GetCommonStatus() | ||
if addonStatus.Phase != aggregatedPhase { | ||
addonStatus.Phase = aggregatedPhase | ||
addonObject.SetCommonStatus(addonStatus) | ||
changed = true | ||
} | ||
} else if unstructOk { | ||
statusPhase, _, err := unstructured.NestedString(unstruct.Object, "status", "phase") | ||
if err != nil { | ||
log.Error(err, "error retrieving status") | ||
return err | ||
} | ||
|
||
if statusPhase != aggregatedPhase { | ||
err := unstructured.SetNestedField(unstruct.Object, aggregatedPhase, "status", "phase") | ||
if err != nil { | ||
log.Error(err, "error retrieving status") | ||
return err | ||
} | ||
changed = true | ||
} | ||
} else { | ||
return fmt.Errorf("instance %T was not an addonsv1alpha1.CommonObject or unstructured."+ | ||
"Unstructured", | ||
src) | ||
} | ||
|
||
if changed == true { | ||
log.WithValues("name", src.GetName()).WithValues("phase", aggregatedPhase).Info("updating status") | ||
err := k.client.Status().Update(ctx, src) | ||
if err != nil { | ||
log.Error(err, "error updating status") | ||
return fmt.Errorf("error error status: %v", err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func aggregateStatus(m map[status.Status]bool) status.Status { | ||
inProgress := m[status.InProgressStatus] | ||
terminating := m[status.TerminatingStatus] | ||
|
||
failed := m[status.FailedStatus] | ||
|
||
if inProgress || terminating { | ||
return status.InProgressStatus | ||
} | ||
|
||
if failed { | ||
return status.FailedStatus | ||
} | ||
|
||
return status.CurrentStatus | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,10 @@ package status | |
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/blang/semver" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
addonsv1alpha1 "sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/addon/pkg/apis/v1alpha1" | ||
|
@@ -58,19 +60,49 @@ func (p *versionCheck) VersionCheck( | |
return true, nil | ||
} | ||
|
||
addonObject, ok := src.(addonsv1alpha1.CommonObject) | ||
if !ok { | ||
return false, fmt.Errorf("object %T was not an addonsv1alpha1.CommonObject", src) | ||
errors := []string{ | ||
fmt.Sprintf("manifest needs operator version >= %v, this operator is version %v", minOperatorVersion.String(), | ||
p.operatorVersion.String()), | ||
} | ||
unstruct, ok := src.(*unstructured.Unstructured) | ||
addonObject, commonOkay := src.(addonsv1alpha1.CommonObject) | ||
|
||
status := addonsv1alpha1.CommonStatus{ | ||
Healthy: false, | ||
Errors: []string{ | ||
fmt.Sprintf("manifest needs operator version >= %v, this operator is version %v", minOperatorVersion.String(), p.operatorVersion.String()), | ||
}, | ||
if ok { | ||
unstructStatus := make(map[string]interface{}) | ||
|
||
s, _, err := unstructured.NestedMap(unstruct.Object, "status") | ||
if err != nil { | ||
log.Error(err, "getting status") | ||
return false, fmt.Errorf("unable to get status from unstructured: %v", err) | ||
} | ||
|
||
unstructStatus = s | ||
unstructStatus["Healthy"] = false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this one should be |
||
unstructStatus["Errors"] = errors | ||
|
||
if !reflect.DeepEqual(unstruct, s) { | ||
err = unstructured.SetNestedField(unstruct.Object, false, "status", "healthy") | ||
if err != nil { | ||
log.Error(err, "unable to updating status in unstructured") | ||
} | ||
|
||
err = unstructured.SetNestedStringSlice(unstruct.Object, errors, "status", "errors") | ||
if err != nil { | ||
log.Error(err, "unable to updating status in unstructured") | ||
} | ||
} | ||
} else if commonOkay { | ||
status := addonObject.GetCommonStatus() | ||
status.Healthy = false | ||
status.Errors = errors | ||
if !reflect.DeepEqual(status, addonObject.GetCommonStatus()) { | ||
status.Healthy = false | ||
status.Errors = errors | ||
addonObject.SetCommonStatus(status) | ||
} | ||
} else { | ||
return false, fmt.Errorf("instance %T was not an addonsv1alpha1.CommonObject or unstructured.Unstructured", src) | ||
} | ||
log.WithValues("name", addonObject.GetName()).WithValues("status", status).Info("updating status") | ||
addonObject.SetCommonStatus(status) | ||
|
||
return false, fmt.Errorf("operator not qualified, manifest needs operator version >= %v", minOperatorVersion.String()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Idea for a future PR: I wonder if we can refactor this into our helper methods ... e.g. GetCommonStatus and SetCommonStatus