-
Notifications
You must be signed in to change notification settings - Fork 65
✨ Plug in unpacker, add Handler #757
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
Merged
Changes from all commits
Commits
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
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
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 |
---|---|---|
|
@@ -17,27 +17,35 @@ limitations under the License. | |
package controllers | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/operator-framework/operator-controller/internal/rukpak/handler" | ||
"github.com/operator-framework/operator-controller/internal/rukpak/util" | ||
"helm.sh/helm/v3/pkg/postrender" | ||
|
||
mmsemver "github.com/Masterminds/semver/v3" | ||
bsemver "github.com/blang/semver/v4" | ||
"github.com/go-logr/logr" | ||
"k8s.io/apimachinery/pkg/api/equality" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
apimeta "k8s.io/apimachinery/pkg/api/meta" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||
apimachyaml "k8s.io/apimachinery/pkg/util/yaml" | ||
"k8s.io/utils/ptr" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/handler" | ||
crhandler "sigs.k8s.io/controller-runtime/pkg/handler" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
|
@@ -51,13 +59,19 @@ import ( | |
catalogfilter "github.com/operator-framework/operator-controller/internal/catalogmetadata/filter" | ||
catalogsort "github.com/operator-framework/operator-controller/internal/catalogmetadata/sort" | ||
rukpakapi "github.com/operator-framework/operator-controller/internal/rukpak/api" | ||
"github.com/operator-framework/operator-controller/internal/rukpak/source" | ||
"github.com/operator-framework/operator-controller/internal/rukpak/storage" | ||
) | ||
|
||
// ClusterExtensionReconciler reconciles a ClusterExtension object | ||
type ClusterExtensionReconciler struct { | ||
client.Client | ||
ReleaseNamespace string | ||
BundleProvider BundleProvider | ||
Unpacker source.Unpacker | ||
ActionClientGetter helmclient.ActionClientGetter | ||
Storage storage.Storage | ||
Handler handler.Handler | ||
Scheme *runtime.Scheme | ||
} | ||
|
||
|
@@ -137,10 +151,62 @@ func (r *ClusterExtensionReconciler) reconcile(ctx context.Context, ext *ocv1alp | |
// Considering only image source. | ||
|
||
// Generate a BundleSource, and then pass this and the ClusterExtension to Unpack | ||
// TODO: | ||
// bs := r.GenerateExpectedBundleSource(*ext, bundle.Image) | ||
// unpacker := NewDefaultUnpacker(msg, namespace, unpackImage) | ||
// unpacker..Unpack(bs, ext) | ||
bs := r.GenerateExpectedBundleSource(*ext, bundle.Image) | ||
unpackResult, err := r.Unpacker.Unpack(ctx, bs, ext) | ||
if err != nil { | ||
return ctrl.Result{}, updateStatusUnpackFailing(&ext.Status, fmt.Errorf("source bundle content: %v", err)) | ||
} | ||
|
||
switch unpackResult.State { | ||
case source.StatePending: | ||
updateStatusUnpackPending(&ext.Status, unpackResult) | ||
// There must be a limit to number of entries if status is stuck at | ||
// unpack pending. | ||
return ctrl.Result{}, nil | ||
case source.StateUnpacking: | ||
updateStatusUnpacking(&ext.Status, unpackResult) | ||
return ctrl.Result{}, nil | ||
case source.StateUnpacked: | ||
if err := r.Storage.Store(ctx, ext, unpackResult.Bundle); err != nil { | ||
return ctrl.Result{}, updateStatusUnpackFailing(&ext.Status, fmt.Errorf("persist bundle content: %v", err)) | ||
} | ||
contentURL, err := r.Storage.URLFor(ctx, ext) | ||
if err != nil { | ||
return ctrl.Result{}, updateStatusUnpackFailing(&ext.Status, fmt.Errorf("get content URL: %v", err)) | ||
} | ||
updateStatusUnpacked(&ext.Status, unpackResult, contentURL) | ||
default: | ||
return ctrl.Result{}, updateStatusUnpackFailing(&ext.Status, fmt.Errorf("unknown unpack state %q: %v", unpackResult.State, err)) | ||
} | ||
|
||
bundleFS, err := r.Storage.Load(ctx, ext) | ||
if err != nil { | ||
meta.SetStatusCondition(&ext.Status.Conditions, metav1.Condition{ | ||
Type: ocv1alpha1.TypeHasValidBundle, | ||
Status: metav1.ConditionFalse, | ||
Reason: ocv1alpha1.ReasonBundleLoadFailed, | ||
Message: err.Error(), | ||
}) | ||
return ctrl.Result{}, err | ||
} | ||
|
||
_, _, err = r.Handler.Handle(ctx, bundleFS, ext) | ||
if err != nil { | ||
meta.SetStatusCondition(&ext.Status.Conditions, metav1.Condition{ | ||
Type: rukpakv1alpha2.TypeInstalled, | ||
Status: metav1.ConditionFalse, | ||
Reason: rukpakv1alpha2.ReasonInstallFailed, | ||
Message: err.Error(), | ||
}) | ||
return ctrl.Result{}, err | ||
} | ||
|
||
ext.SetNamespace(r.ReleaseNamespace) | ||
_, err = r.ActionClientGetter.ActionClientFor(ext) | ||
if err != nil { | ||
setInstalledStatusConditionFailed(&ext.Status.Conditions, fmt.Sprintf("%s:%v", ocv1alpha1.ReasonErrorGettingClient, err), ext.Generation) | ||
return ctrl.Result{}, err | ||
} | ||
|
||
// set the status of the cluster extension based on the respective bundle deployment status conditions. | ||
return ctrl.Result{}, nil | ||
|
@@ -286,7 +352,7 @@ func (r *ClusterExtensionReconciler) SetupWithManager(mgr ctrl.Manager) error { | |
err := ctrl.NewControllerManagedBy(mgr). | ||
For(&ocv1alpha1.ClusterExtension{}). | ||
Watches(&catalogd.Catalog{}, | ||
handler.EnqueueRequestsFromMapFunc(clusterExtensionRequestsForCatalog(mgr.GetClient(), mgr.GetLogger()))). | ||
crhandler.EnqueueRequestsFromMapFunc(clusterExtensionRequestsForCatalog(mgr.GetClient(), mgr.GetLogger()))). | ||
Owns(&rukpakv1alpha2.BundleDeployment{}). | ||
Complete(r) | ||
|
||
|
@@ -297,7 +363,7 @@ func (r *ClusterExtensionReconciler) SetupWithManager(mgr ctrl.Manager) error { | |
} | ||
|
||
// Generate reconcile requests for all cluster extensions affected by a catalog change | ||
func clusterExtensionRequestsForCatalog(c client.Reader, logger logr.Logger) handler.MapFunc { | ||
func clusterExtensionRequestsForCatalog(c client.Reader, logger logr.Logger) crhandler.MapFunc { | ||
return func(ctx context.Context, _ client.Object) []reconcile.Request { | ||
// no way of associating an extension to a catalog so create reconcile requests for everything | ||
clusterExtensions := ocv1alpha1.ClusterExtensionList{} | ||
|
@@ -417,3 +483,33 @@ func (r *ClusterExtensionReconciler) getInstalledVersion(ctx context.Context, cl | |
} | ||
return existingVersionSemver, nil | ||
} | ||
|
||
type postrenderer struct { | ||
labels map[string]string | ||
cascade postrender.PostRenderer | ||
} | ||
|
||
func (p *postrenderer) Run(renderedManifests *bytes.Buffer) (*bytes.Buffer, error) { | ||
var buf bytes.Buffer | ||
dec := apimachyaml.NewYAMLOrJSONDecoder(renderedManifests, 1024) | ||
for { | ||
obj := unstructured.Unstructured{} | ||
err := dec.Decode(&obj) | ||
if errors.Is(err, io.EOF) { | ||
break | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
obj.SetLabels(util.MergeMaps(obj.GetLabels(), p.labels)) | ||
b, err := obj.MarshalJSON() | ||
if err != nil { | ||
return nil, err | ||
} | ||
buf.Write(b) | ||
} | ||
if p.cascade != nil { | ||
return p.cascade.Run(&buf) | ||
} | ||
return &buf, nil | ||
} | ||
Comment on lines
+486
to
+515
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. What's this for? Something moving forward? |
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,74 @@ | ||
/* | ||
Copyright 2023. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package controllers | ||
|
||
import ( | ||
ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1" | ||
rukpakapi "github.com/operator-framework/operator-controller/internal/rukpak/api" | ||
"github.com/operator-framework/operator-controller/internal/rukpak/source" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func updateStatusUnpackFailing(status *ocv1alpha1.ClusterExtensionStatus, err error) error { | ||
status.ResolvedBundle = nil | ||
status.InstalledBundle = nil | ||
meta.SetStatusCondition(&status.Conditions, metav1.Condition{ | ||
Type: rukpakapi.TypeUnpacked, | ||
Status: metav1.ConditionFalse, | ||
Reason: rukpakapi.ReasonUnpackFailed, | ||
Message: err.Error(), | ||
}) | ||
return err | ||
} | ||
|
||
// TODO: verify if we need to update the installBundle status or leave it as is. | ||
func updateStatusUnpackPending(status *ocv1alpha1.ClusterExtensionStatus, result *source.Result) { | ||
status.ResolvedBundle = nil | ||
status.InstalledBundle = nil | ||
meta.SetStatusCondition(&status.Conditions, metav1.Condition{ | ||
Type: rukpakapi.TypeUnpacked, | ||
Status: metav1.ConditionFalse, | ||
Reason: rukpakapi.ReasonUnpackPending, | ||
Message: result.Message, | ||
}) | ||
} | ||
|
||
// TODO: verify if we need to update the installBundle status or leave it as is. | ||
func updateStatusUnpacking(status *ocv1alpha1.ClusterExtensionStatus, result *source.Result) { | ||
status.ResolvedBundle = nil | ||
status.InstalledBundle = nil | ||
meta.SetStatusCondition(&status.Conditions, metav1.Condition{ | ||
Type: rukpakapi.TypeUnpacked, | ||
Status: metav1.ConditionFalse, | ||
Reason: rukpakapi.ReasonUnpacking, | ||
Message: result.Message, | ||
}) | ||
} | ||
|
||
func updateStatusUnpacked(status *ocv1alpha1.ClusterExtensionStatus, result *source.Result, contentURL string) { | ||
// TODO: Expose content URL through CE status. | ||
status.ResolvedBundle = &ocv1alpha1.BundleMetadata{ | ||
Name: result.ResolvedSource.Image.Ref, | ||
} | ||
meta.SetStatusCondition(&status.Conditions, metav1.Condition{ | ||
Type: rukpakapi.TypeUnpacked, | ||
Status: metav1.ConditionTrue, | ||
Reason: rukpakapi.ReasonUnpackSuccessful, | ||
Message: result.Message, | ||
}) | ||
} |
Oops, something went wrong.
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.
Nit: I really think there should be a blank line after this.