From 0a39e1197a4b64fcdbc2f4e10dba2787e7c02fad Mon Sep 17 00:00:00 2001 From: Philip Wu <81585886+philipwu08@users.noreply.github.com> Date: Mon, 28 Mar 2022 11:58:10 -0400 Subject: [PATCH] Update lastUpdateTime for appsub on reconcile (#142) Signed-off-by: Philip Wu --- cmd/scripts/getLastUpdateTime.sh | 107 ++++++++++++++++++ pkg/subscriber/git/git_subscriber_item.go | 2 + .../helmrepo/helm_subscriber_item.go | 2 + .../objectbucket_subscriber_item.go | 2 + pkg/utils/subscription.go | 15 +++ 5 files changed, 128 insertions(+) create mode 100755 cmd/scripts/getLastUpdateTime.sh diff --git a/cmd/scripts/getLastUpdateTime.sh b/cmd/scripts/getLastUpdateTime.sh new file mode 100755 index 00000000..b219a17f --- /dev/null +++ b/cmd/scripts/getLastUpdateTime.sh @@ -0,0 +1,107 @@ +#! /bin/bash + +usage () { + echo "getLastUpdateTime.sh is to get the last update time for an ApspSub on a given managed cluster" + echo "" + echo "Options:" + echo " -c managed cluster name" + echo " -s AppSub Namespace" + echo " -n AppSub Name" + echo " -h Help" + echo "" + echo "Example: ./getLastUpdateTime.sh -c cluster1 -s appsubNS1 -n appsub1" +} + +check_dependency () { + which jq > /dev/null + if [ $? -ne 0 ]; then + echo "jq is not installed." + exit 1 + fi +} + +check_dependency + +if [ "$#" -lt 1 ]; then + usage + exit 0 +fi + +while getopts "h:c:s:n:" arg; do + case $arg in + c) + cluster="${OPTARG}" + ;; + s) + appNs="${OPTARG}" + ;; + n) + appName="${OPTARG}" + ;; + :) + usage + exit 0 + ;; + *) + usage + exit 0 + ;; + esac +done + +echo "==== Validating HUB Cluster Access ====" +kubectl cluster-info > /dev/null +if [ $? -ne 0 ]; then + echo "Hub cluster not accessible." + exit 1 +fi + +echo "==== Validating Managed cluster: ${cluster} ====" +kubectl get managedcluster $cluster > /dev/null +if [ $? -ne 0 ]; then + echo "Managed cluster '${cluster}' not found." + exit 1 +fi + +echo "==== Validating AppSub on Hub: ${appNs}/${appName} ====" +kubectl get appsub -n $appNs $appName > /dev/null +if [ $? -ne 0 ]; then + echo "AppSub '${appNs}/${appName}' not found on the Hub." + exit 1 +fi + +# Delete if there is an existing managedclusterview +kubectl get managedclusterview -n $cluster getappsub > /dev/null 2>&1 +if [ $? -eq 0 ]; then + kubectl delete managedclusterview -n ${cluster} getappsub --ignore-not-found=true > /dev/null 2>&1 +fi + + +kubectl apply -f - -o yaml > /dev/null << EOF +apiVersion: view.open-cluster-management.io/v1beta1 +kind: ManagedClusterView +metadata: + name: getappsub + namespace: ${cluster} +spec: + scope: + apiGroup: apps.open-cluster-management.io + kind: Subscription + version: v1alpha1 + resource: subscriptions + name: ${appName} + namespace: ${appNs} +EOF + +# Check status of the managed cluster view +result=($(kubectl get managedclusterview -n ${cluster} getappsub -o jsonpath='{.status.conditions}' | jq --raw-output .[].status)) +if [ "$result" = "False" ]; then + echo "AppSub '${appNs}/${appName}' not found on managed cluster '${cluster}'" + exit 1 +fi + +echo -n "LastUpdateTime: " +kubectl get managedclusterview -n ${cluster} getappsub -o jsonpath='{.status.result}' | jq --raw-output .status.lastUpdateTime + +kubectl delete managedclusterview -n ${cluster} getappsub --ignore-not-found=true > /dev/null 2>&1 + diff --git a/pkg/subscriber/git/git_subscriber_item.go b/pkg/subscriber/git/git_subscriber_item.go index 87f5ada9..c62f63bb 100644 --- a/pkg/subscriber/git/git_subscriber_item.go +++ b/pkg/subscriber/git/git_subscriber_item.go @@ -185,6 +185,8 @@ func (ghsi *SubscriberItem) doSubscription() error { defer klog.Info("exit doSubscription: ", hostkey.String()) + utils.UpdateLastUpdateTime(ghsi.synchronizer.GetLocalClient(), ghsi.Subscription) + // If webhook is enabled, don't do anything until next reconcilitation. if ghsi.webhookEnabled { klog.Infof("Git Webhook is enabled on subscription %s.", ghsi.Subscription.Name) diff --git a/pkg/subscriber/helmrepo/helm_subscriber_item.go b/pkg/subscriber/helmrepo/helm_subscriber_item.go index 56be402c..61acb2eb 100644 --- a/pkg/subscriber/helmrepo/helm_subscriber_item.go +++ b/pkg/subscriber/helmrepo/helm_subscriber_item.go @@ -179,6 +179,8 @@ func (hrsi *SubscriberItem) doSubscription() { var err error + utils.UpdateLastUpdateTime(hrsi.synchronizer.GetLocalClient(), hrsi.Subscription) + //Update the secret and config map if hrsi.Channel != nil { sec, cm := utils.FetchChannelReferences(hrsi.synchronizer.GetRemoteNonCachedClient(), *hrsi.Channel) diff --git a/pkg/subscriber/objectbucket/objectbucket_subscriber_item.go b/pkg/subscriber/objectbucket/objectbucket_subscriber_item.go index ed308013..292acc4a 100644 --- a/pkg/subscriber/objectbucket/objectbucket_subscriber_item.go +++ b/pkg/subscriber/objectbucket/objectbucket_subscriber_item.go @@ -127,6 +127,8 @@ func (obsi *SubscriberItem) CompareOjbectStoreStatus(initObjectStoreErr error) b } func (obsi *SubscriberItem) getChannelConfig(primary bool) (endpoint, accessKeyID, secretAccessKey, region string, err error) { + utils.UpdateLastUpdateTime(obsi.synchronizer.GetLocalClient(), obsi.Subscription) + channel := obsi.Channel if !primary { diff --git a/pkg/utils/subscription.go b/pkg/utils/subscription.go index 99d9494b..0c01dace 100644 --- a/pkg/utils/subscription.go +++ b/pkg/utils/subscription.go @@ -687,6 +687,7 @@ func ValidatePackagesInSubscriptionStatus(statusClient client.StatusClient, sub klog.V(10).Info("Updating", sub.Status, sub.Status.Statuses["/"]) sub.Status.LastUpdateTime = metav1.Now() + err = statusClient.Status().Update(context.TODO(), sub) // want to print out the error log before leave if err != nil { @@ -697,6 +698,20 @@ func ValidatePackagesInSubscriptionStatus(statusClient client.StatusClient, sub return err } +func UpdateLastUpdateTime(clt client.Client, instance *appv1.Subscription) { + curSub := &appv1.Subscription{} + if err := clt.Get(context.TODO(), types.NamespacedName{Name: instance.GetName(), Namespace: instance.GetNamespace()}, curSub); err != nil { + klog.Warning("Failed to get appsub to update LastUpdateTime", err) + return + } + + curSub.Status.LastUpdateTime = metav1.Now() + + if err := clt.Status().Update(context.TODO(), curSub); err != nil { + klog.Warning("Failed to update LastUpdateTime", err) + } +} + // OverrideResourceBySubscription alter the given template with overrides func OverrideResourceBySubscription(template *unstructured.Unstructured, pkgName string, instance *appv1.Subscription) (*unstructured.Unstructured, error) {