-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create release manifest via a job Signed-off-by: Atanas Dinov <[email protected]> * Extract release manifest image source Signed-off-by: Atanas Dinov <[email protected]> * Extract service account name Signed-off-by: Atanas Dinov <[email protected]> --------- Signed-off-by: Atanas Dinov <[email protected]>
- Loading branch information
1 parent
72ae0e0
commit 8db0eb2
Showing
6 changed files
with
137 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,43 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
lifecyclev1alpha1 "github.com/suse-edge/upgrade-controller/api/v1alpha1" | ||
"github.com/suse-edge/upgrade-controller/internal/upgrade" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
"k8s.io/client-go/util/retry" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
var errReleaseManifestNotFound = fmt.Errorf("release manifest not found") | ||
|
||
func (r *UpgradePlanReconciler) retrieveReleaseManifest(ctx context.Context, upgradePlan *lifecyclev1alpha1.UpgradePlan) (*lifecyclev1alpha1.ReleaseManifest, error) { | ||
manifests := &lifecyclev1alpha1.ReleaseManifestList{} | ||
listOpts := &client.ListOptions{ | ||
Namespace: upgradePlan.Namespace, | ||
} | ||
if err := r.List(ctx, manifests, listOpts); err != nil { | ||
return nil, fmt.Errorf("listing release manifests in cluster: %w", err) | ||
} | ||
|
||
for _, manifest := range manifests.Items { | ||
if manifest.Spec.ReleaseVersion == upgradePlan.Spec.ReleaseVersion { | ||
return &manifest, nil | ||
} | ||
} | ||
|
||
return nil, errReleaseManifestNotFound | ||
} | ||
|
||
func (r *UpgradePlanReconciler) createReleaseManifest(ctx context.Context, upgradePlan *lifecyclev1alpha1.UpgradePlan) error { | ||
annotations := upgrade.PlanIdentifierAnnotations(upgradePlan.Name, upgradePlan.Namespace) | ||
job := upgrade.ReleaseManifestInstallJob(r.ReleaseManifestImage, upgradePlan.Spec.ReleaseVersion, r.ServiceAccount, upgradePlan.Namespace, annotations) | ||
|
||
// Retry the creation since a previously failed job could be in the process of deletion due to TTL | ||
return retry.OnError(wait.Backoff{Steps: 5, Duration: 500 * time.Millisecond}, | ||
func(err error) bool { return true }, | ||
func() error { return r.createObject(ctx, upgradePlan, job) }) | ||
} |
This file contains 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 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,49 @@ | ||
package upgrade | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
batchv1 "k8s.io/api/batch/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func ReleaseManifestInstallJob(image, version, serviceAccount, namespace string, annotations map[string]string) *batchv1.Job { | ||
version = strings.TrimPrefix(version, "v") | ||
workloadName := fmt.Sprintf("apply-release-manifest-%s", strings.ReplaceAll(version, ".", "-")) | ||
image = fmt.Sprintf("%s:%s", image, version) | ||
ttl := int32(0) | ||
|
||
return &batchv1.Job{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: "batch/v1", | ||
Kind: "Job", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: workloadName, | ||
Namespace: namespace, | ||
Annotations: annotations, | ||
}, | ||
Spec: batchv1.JobSpec{ | ||
Template: corev1.PodTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: workloadName, | ||
Namespace: namespace, | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
{ | ||
Name: workloadName, | ||
Image: image, | ||
Args: []string{"apply", "-f", "release_manifest.yaml"}, | ||
}, | ||
}, | ||
RestartPolicy: "OnFailure", | ||
ServiceAccountName: serviceAccount, | ||
}, | ||
}, | ||
TTLSecondsAfterFinished: &ttl, | ||
}, | ||
} | ||
} |