Skip to content

Commit

Permalink
Update lastUpdateTime for appsub on reconcile (#142)
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Wu <phwu@redhat.com>
  • Loading branch information
philipwu08 authored Mar 28, 2022
1 parent 6b93a64 commit 0a39e11
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
107 changes: 107 additions & 0 deletions cmd/scripts/getLastUpdateTime.sh
Original file line number Diff line number Diff line change
@@ -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

2 changes: 2 additions & 0 deletions pkg/subscriber/git/git_subscriber_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions pkg/subscriber/helmrepo/helm_subscriber_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions pkg/subscriber/objectbucket/objectbucket_subscriber_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions pkg/utils/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down

0 comments on commit 0a39e11

Please sign in to comment.