-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GitHub client and download release assets
Signed-off-by: Roman Hros <[email protected]>
- Loading branch information
1 parent
3174fbd
commit 87c2426
Showing
369 changed files
with
80,665 additions
and
19 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
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,11 @@ | ||
apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: cluster-stack-variables | ||
namespace: system | ||
type: Opaque | ||
data: | ||
git-provider: ${GIT_PROVIDER_B64:=""} | ||
git-org-name: ${GIT_ORG_NAME_B64:=""} | ||
git-repo-name: ${GIT_REPOSITORY_NAME_B64:=""} | ||
git-access-token: ${GIT_ACCESS_TOKEN_B64:=""} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
resources: | ||
- manager.yaml | ||
- credentials.yaml |
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 |
---|---|---|
|
@@ -18,7 +18,14 @@ package controller | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"sync" | ||
"time" | ||
|
||
githubclient "github.com/SovereignCloudStack/cluster-stack-operator/pkg/github/client" | ||
"github.com/SovereignCloudStack/cluster-stack-operator/pkg/release" | ||
apiv1alpha1 "github.com/sovereignCloudStack/cluster-stack-provider-openstack/api/v1alpha1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
|
@@ -29,7 +36,10 @@ import ( | |
// OpenStackClusterStackReleaseReconciler reconciles a OpenStackClusterStackRelease object. | ||
type OpenStackClusterStackReleaseReconciler struct { | ||
client.Client | ||
Scheme *runtime.Scheme | ||
Scheme *runtime.Scheme | ||
GitHubClientFactory githubclient.Factory | ||
ReleaseDirectory string | ||
openStackClusterStackRelDownloadDirectoryMutex sync.Mutex | ||
} | ||
|
||
//+kubebuilder:rbac:groups=infrastructure.clusterstack.x-k8s.io,resources=openstackclusterstackreleases,verbs=get;list;watch;create;update;patch;delete | ||
|
@@ -46,16 +56,75 @@ type OpenStackClusterStackReleaseReconciler struct { | |
// For more details, check Reconcile and its Result here: | ||
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile | ||
func (r *OpenStackClusterStackReleaseReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
_ = log.FromContext(ctx) | ||
logger := log.FromContext(ctx) | ||
|
||
openstackclusterstackrelease := &apiv1alpha1.OpenStackClusterStackRelease{} | ||
_ = r.Client.Get(ctx, req.NamespacedName, openstackclusterstackrelease) | ||
err := r.Client.Get(ctx, req.NamespacedName, openstackclusterstackrelease) | ||
if err != nil { | ||
return ctrl.Result{}, fmt.Errorf("failed to get OpenStackClusterStackRelease %s/%s: %w", req.Namespace, req.Name, err) | ||
} | ||
|
||
// TODO(user): your logic here | ||
gc, err := r.GitHubClientFactory.NewClient(ctx) | ||
if err != nil { | ||
return ctrl.Result{}, fmt.Errorf("failed to create Github client: %w", err) | ||
} | ||
|
||
// name of OpenStackClusterStackRelease object is same as the release tag | ||
releaseTag := openstackclusterstackrelease.Name | ||
|
||
releaseAssets, download, err := release.New(releaseTag, r.ReleaseDirectory) | ||
if err != nil { | ||
return ctrl.Result{RequeueAfter: 1 * time.Minute}, fmt.Errorf("failed to create release: %w", err) | ||
} | ||
|
||
if download { | ||
// this is the point where we download the release from github | ||
// acquire lock so that only one reconcile loop can download the release | ||
r.openStackClusterStackRelDownloadDirectoryMutex.Lock() | ||
|
||
if err := downloadReleaseAssets(ctx, releaseTag, releaseAssets.LocalDownloadPath, gc); err != nil { | ||
return ctrl.Result{}, fmt.Errorf("failed to download release assets: %w", err) | ||
} | ||
|
||
r.openStackClusterStackRelDownloadDirectoryMutex.Unlock() | ||
|
||
// requeue to make sure release assets can be accessed | ||
return ctrl.Result{Requeue: true}, nil | ||
} | ||
|
||
logger.Info("OpenStackClusterStackRelease status", "ready", openstackclusterstackrelease.Status.Ready) | ||
openstackclusterstackrelease.Status.Ready = true | ||
err = r.Status().Update(ctx, openstackclusterstackrelease) | ||
if err != nil { | ||
return ctrl.Result{}, fmt.Errorf("failed to update OpenStackClusterStackRelease status") | ||
} | ||
logger.Info("OpenStackClusterStackRelease ready") | ||
|
||
return ctrl.Result{}, nil | ||
} | ||
|
||
func downloadReleaseAssets(ctx context.Context, releaseTag, downloadPath string, gc githubclient.Client) error { | ||
repoRelease, resp, err := gc.GetReleaseByTag(ctx, releaseTag) | ||
if err != nil { | ||
return fmt.Errorf("failed to fetch release tag %q: %w", releaseTag, err) | ||
} | ||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("failed to fetch release tag %s with status code %d", releaseTag, resp.StatusCode) | ||
} | ||
|
||
assetlist := []string{"metadata.yaml", "node-images.yaml"} | ||
|
||
if err := gc.DownloadReleaseAssets(ctx, repoRelease, downloadPath, assetlist); err != nil { | ||
// if download failed for some reason, delete the release directory so that it can be retried in the next reconciliation | ||
if err := os.RemoveAll(downloadPath); err != nil { | ||
return fmt.Errorf("failed to remove release: %w", err) | ||
} | ||
return fmt.Errorf("failed to download release assets: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *OpenStackClusterStackReleaseReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.