Skip to content

Commit cae1cf1

Browse files
authored
BundleSource cleanup (#1061)
Signed-off-by: dtfranz <[email protected]>
1 parent 3f06480 commit cae1cf1

File tree

7 files changed

+65
-142
lines changed

7 files changed

+65
-142
lines changed

internal/controllers/clusterextension_controller.go

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ import (
6767
"github.com/operator-framework/operator-controller/internal/conditionsets"
6868
"github.com/operator-framework/operator-controller/internal/labels"
6969
"github.com/operator-framework/operator-controller/internal/resolve"
70-
"github.com/operator-framework/operator-controller/internal/rukpak/bundledeployment"
7170
"github.com/operator-framework/operator-controller/internal/rukpak/convert"
7271
"github.com/operator-framework/operator-controller/internal/rukpak/preflights/crdupgradesafety"
7372
rukpaksource "github.com/operator-framework/operator-controller/internal/rukpak/source"
@@ -278,12 +277,14 @@ func (r *ClusterExtensionReconciler) reconcile(ctx context.Context, ext *ocv1alp
278277
ext.Status.ResolvedBundle = bundleutil.MetadataFor(resolvedBundle.Name, *resolvedBundleVersion)
279278
setResolvedStatusConditionSuccess(ext, fmt.Sprintf("resolved to %q", resolvedBundle.Image))
280279

281-
// Generate a BundleDeployment from the ClusterExtension to Unpack.
282-
// Note: The BundleDeployment here is not a k8s API, its a simple Go struct which
283-
// necessary embedded values.
284-
bd := r.generateBundleDeploymentForUnpack(resolvedBundle.Image, ext)
280+
bundleSource := &rukpaksource.BundleSource{
281+
Type: rukpaksource.SourceTypeImage,
282+
Image: &rukpaksource.ImageSource{
283+
Ref: resolvedBundle.Image,
284+
},
285+
}
285286
l.V(1).Info("unpacking resolved bundle")
286-
unpackResult, err := r.Unpacker.Unpack(ctx, bd)
287+
unpackResult, err := r.Unpacker.Unpack(ctx, bundleSource)
287288
if err != nil {
288289
setStatusUnpackFailed(ext, err.Error())
289290
return ctrl.Result{}, err
@@ -505,21 +506,6 @@ func SetDeprecationStatus(ext *ocv1alpha1.ClusterExtension, bundleName string, d
505506
}
506507
}
507508

508-
func (r *ClusterExtensionReconciler) generateBundleDeploymentForUnpack(bundlePath string, ce *ocv1alpha1.ClusterExtension) *bundledeployment.BundleDeployment {
509-
return &bundledeployment.BundleDeployment{
510-
Name: ce.Name,
511-
Spec: bundledeployment.BundleDeploymentSpec{
512-
InstallNamespace: ce.Spec.InstallNamespace,
513-
Source: bundledeployment.BundleSource{
514-
Type: bundledeployment.SourceTypeImage,
515-
Image: &bundledeployment.ImageSource{
516-
Ref: bundlePath,
517-
},
518-
},
519-
},
520-
}
521-
}
522-
523509
// SetupWithManager sets up the controller with the Manager.
524510
func (r *ClusterExtensionReconciler) SetupWithManager(mgr ctrl.Manager) error {
525511
controller, err := ctrl.NewControllerManagedBy(mgr).

internal/controllers/clusterextension_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestClusterExtensionResolutionSucceeds(t *testing.T) {
8989
cl, reconciler := newClientAndReconciler(t, nil)
9090
mockUnpacker := unpacker.(*MockUnpacker)
9191
// Set up the Unpack method to return a result with StateUnpacked
92-
mockUnpacker.On("Unpack", mock.Anything, mock.AnythingOfType("*bundledeployment.BundleDeployment")).Return(&source.Result{
92+
mockUnpacker.On("Unpack", mock.Anything, mock.AnythingOfType("*source.BundleSource")).Return(&source.Result{
9393
State: source.StatePending,
9494
}, nil)
9595

internal/controllers/clusterextension_registryv1_validation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func TestClusterExtensionRegistryV1DisallowDependencies(t *testing.T) {
100100
})
101101
mockUnpacker := unpacker.(*MockUnpacker)
102102
// Set up the Unpack method to return a result with StatePending
103-
mockUnpacker.On("Unpack", mock.Anything, mock.AnythingOfType("*v1alpha2.BundleDeployment")).Return(&source.Result{
103+
mockUnpacker.On("Unpack", mock.Anything, mock.AnythingOfType("*source.BundleSource")).Return(&source.Result{
104104
State: source.StatePending,
105105
}, nil)
106106

internal/controllers/suite_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737

3838
ocv1alpha1 "github.com/operator-framework/operator-controller/api/v1alpha1"
3939
"github.com/operator-framework/operator-controller/internal/controllers"
40-
bd "github.com/operator-framework/operator-controller/internal/rukpak/bundledeployment"
4140
"github.com/operator-framework/operator-controller/internal/rukpak/source"
4241
)
4342

@@ -47,12 +46,12 @@ type MockUnpacker struct {
4746
}
4847

4948
// Unpack mocks the Unpack method
50-
func (m *MockUnpacker) Unpack(ctx context.Context, bd *bd.BundleDeployment) (*source.Result, error) {
51-
args := m.Called(ctx, bd)
49+
func (m *MockUnpacker) Unpack(ctx context.Context, bundle *source.BundleSource) (*source.Result, error) {
50+
args := m.Called(ctx, bundle)
5251
return args.Get(0).(*source.Result), args.Error(1)
5352
}
5453

55-
func (m *MockUnpacker) Cleanup(ctx context.Context, bundle *bd.BundleDeployment) error {
54+
func (m *MockUnpacker) Cleanup(ctx context.Context, bundle *source.BundleSource) error {
5655
//TODO implement me
5756
panic("implement me")
5857
}

internal/rukpak/bundledeployment/bundledeployment.go

Lines changed: 0 additions & 83 deletions
This file was deleted.

internal/rukpak/source/image_registry.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,23 @@ import (
2020
"github.com/google/go-containerregistry/pkg/v1/remote"
2121
apimacherrors "k8s.io/apimachinery/pkg/util/errors"
2222
"sigs.k8s.io/controller-runtime/pkg/log"
23-
24-
bd "github.com/operator-framework/operator-controller/internal/rukpak/bundledeployment"
2523
)
2624

25+
// SourceTypeImage is the identifier for image-type bundle sources
26+
const SourceTypeImage SourceType = "image"
27+
28+
type ImageSource struct {
29+
// Ref contains the reference to a container image containing Bundle contents.
30+
Ref string
31+
// ImagePullSecretName contains the name of the image pull secret in the namespace that the provisioner is deployed.
32+
ImagePullSecretName string
33+
// InsecureSkipTLSVerify indicates that TLS certificate validation should be skipped.
34+
// If this option is specified, the HTTPS protocol will still be used to
35+
// fetch the specified image reference.
36+
// This should not be used in a production environment.
37+
InsecureSkipTLSVerify bool
38+
}
39+
2740
// Unrecoverable represents an error that can not be recovered
2841
// from without user intervention. When this error is returned
2942
// the request should not be requeued.
@@ -43,25 +56,25 @@ type ImageRegistry struct {
4356
CaCertPool *x509.CertPool
4457
}
4558

46-
func (i *ImageRegistry) Unpack(ctx context.Context, bundle *bd.BundleDeployment) (*Result, error) {
59+
func (i *ImageRegistry) Unpack(ctx context.Context, bundle *BundleSource) (*Result, error) {
4760
l := log.FromContext(ctx)
48-
if bundle.Spec.Source.Type != bd.SourceTypeImage {
49-
panic(fmt.Sprintf("programmer error: source type %q is unable to handle specified bundle source type %q", bd.SourceTypeImage, bundle.Spec.Source.Type))
61+
if bundle.Type != SourceTypeImage {
62+
panic(fmt.Sprintf("programmer error: source type %q is unable to handle specified bundle source type %q", SourceTypeImage, bundle.Type))
5063
}
5164

52-
if bundle.Spec.Source.Image == nil {
65+
if bundle.Image == nil {
5366
return nil, NewUnrecoverable(fmt.Errorf("error parsing bundle, bundle %s has a nil image source", bundle.Name))
5467
}
5568

56-
imgRef, err := name.ParseReference(bundle.Spec.Source.Image.Ref)
69+
imgRef, err := name.ParseReference(bundle.Image.Ref)
5770
if err != nil {
5871
return nil, NewUnrecoverable(fmt.Errorf("error parsing image reference: %w", err))
5972
}
6073

6174
remoteOpts := []remote.Option{}
62-
if bundle.Spec.Source.Image.ImagePullSecretName != "" {
75+
if bundle.Image.ImagePullSecretName != "" {
6376
chainOpts := k8schain.Options{
64-
ImagePullSecrets: []string{bundle.Spec.Source.Image.ImagePullSecretName},
77+
ImagePullSecrets: []string{bundle.Image.ImagePullSecretName},
6578
Namespace: i.AuthNamespace,
6679
// TODO: Do we want to use any secrets that are included in the rukpak service account?
6780
// If so, we will need to add the permission to get service accounts and specify
@@ -83,7 +96,7 @@ func (i *ImageRegistry) Unpack(ctx context.Context, bundle *bd.BundleDeployment)
8396
MinVersion: tls.VersionTLS12,
8497
} // nolint:gosec
8598
}
86-
if bundle.Spec.Source.Image.InsecureSkipTLSVerify {
99+
if bundle.Image.InsecureSkipTLSVerify {
87100
transport.TLSClientConfig.InsecureSkipVerify = true // nolint:gosec
88101
}
89102
if i.CaCertPool != nil {
@@ -146,19 +159,19 @@ func wrapUnrecoverable(err error, isUnrecoverable bool) error {
146159
return err
147160
}
148161

149-
func (i *ImageRegistry) Cleanup(_ context.Context, bundle *bd.BundleDeployment) error {
162+
func (i *ImageRegistry) Cleanup(_ context.Context, bundle *BundleSource) error {
150163
return os.RemoveAll(filepath.Join(i.BaseCachePath, bundle.Name))
151164
}
152165

153-
func unpackedResult(fsys fs.FS, bundle *bd.BundleDeployment, ref string) *Result {
166+
func unpackedResult(fsys fs.FS, bundle *BundleSource, ref string) *Result {
154167
return &Result{
155168
Bundle: fsys,
156-
ResolvedSource: &bd.BundleSource{
157-
Type: bd.SourceTypeImage,
158-
Image: &bd.ImageSource{
169+
ResolvedSource: &BundleSource{
170+
Type: SourceTypeImage,
171+
Image: &ImageSource{
159172
Ref: ref,
160-
ImagePullSecretName: bundle.Spec.Source.Image.ImagePullSecretName,
161-
InsecureSkipTLSVerify: bundle.Spec.Source.Image.InsecureSkipTLSVerify,
173+
ImagePullSecretName: bundle.Image.ImagePullSecretName,
174+
InsecureSkipTLSVerify: bundle.Image.InsecureSkipTLSVerify,
162175
},
163176
},
164177
State: StateUnpacked,

internal/rukpak/source/unpacker.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"io/fs"
77

88
"sigs.k8s.io/controller-runtime/pkg/manager"
9-
10-
bd "github.com/operator-framework/operator-controller/internal/rukpak/bundledeployment"
119
)
1210

1311
// Unpacker unpacks bundle content, either synchronously or asynchronously and
@@ -25,8 +23,8 @@ import (
2523
// specifications. A source should treat a bundle root directory as an opaque
2624
// file tree and delegate bundle format concerns to bundle parsers.
2725
type Unpacker interface {
28-
Unpack(context.Context, *bd.BundleDeployment) (*Result, error)
29-
Cleanup(context.Context, *bd.BundleDeployment) error
26+
Unpack(context.Context, *BundleSource) (*Result, error)
27+
Cleanup(context.Context, *BundleSource) error
3028
}
3129

3230
// Result conveys progress information about unpacking bundle content.
@@ -43,7 +41,7 @@ type Result struct {
4341
// For example, resolved image sources should reference a container image
4442
// digest rather than an image tag, and git sources should reference a
4543
// commit hash rather than a branch or tag.
46-
ResolvedSource *bd.BundleSource
44+
ResolvedSource *BundleSource
4745

4846
// State is the current state of unpacking the bundle content.
4947
State State
@@ -69,28 +67,38 @@ const (
6967
StateUnpacked State = "Unpacked"
7068
)
7169

70+
type SourceType string
71+
72+
type BundleSource struct {
73+
Name string
74+
// Type defines the kind of Bundle content being sourced.
75+
Type SourceType
76+
// Image is the bundle image that backs the content of this bundle.
77+
Image *ImageSource
78+
}
79+
7280
type unpacker struct {
73-
sources map[bd.SourceType]Unpacker
81+
sources map[SourceType]Unpacker
7482
}
7583

7684
// NewUnpacker returns a new composite Source that unpacks bundles using the source
7785
// mapping provided by the configured sources.
78-
func NewUnpacker(sources map[bd.SourceType]Unpacker) Unpacker {
86+
func NewUnpacker(sources map[SourceType]Unpacker) Unpacker {
7987
return &unpacker{sources: sources}
8088
}
8189

82-
func (s *unpacker) Unpack(ctx context.Context, bundle *bd.BundleDeployment) (*Result, error) {
83-
source, ok := s.sources[bundle.Spec.Source.Type]
90+
func (s *unpacker) Unpack(ctx context.Context, bundle *BundleSource) (*Result, error) {
91+
source, ok := s.sources[bundle.Type]
8492
if !ok {
85-
return nil, fmt.Errorf("source type %q not supported", bundle.Spec.Source.Type)
93+
return nil, fmt.Errorf("source type %q not supported", bundle.Type)
8694
}
8795
return source.Unpack(ctx, bundle)
8896
}
8997

90-
func (s *unpacker) Cleanup(ctx context.Context, bundle *bd.BundleDeployment) error {
91-
source, ok := s.sources[bundle.Spec.Source.Type]
98+
func (s *unpacker) Cleanup(ctx context.Context, bundle *BundleSource) error {
99+
source, ok := s.sources[bundle.Type]
92100
if !ok {
93-
return fmt.Errorf("source type %q not supported", bundle.Spec.Source.Type)
101+
return fmt.Errorf("source type %q not supported", bundle.Type)
94102
}
95103
return source.Cleanup(ctx, bundle)
96104
}
@@ -99,8 +107,8 @@ func (s *unpacker) Cleanup(ctx context.Context, bundle *bd.BundleDeployment) err
99107
// a default source mapping with built-in implementations of all of the supported
100108
// source types.
101109
func NewDefaultUnpacker(mgr manager.Manager, namespace, cacheDir string) (Unpacker, error) {
102-
return NewUnpacker(map[bd.SourceType]Unpacker{
103-
bd.SourceTypeImage: &ImageRegistry{
110+
return NewUnpacker(map[SourceType]Unpacker{
111+
SourceTypeImage: &ImageRegistry{
104112
BaseCachePath: cacheDir,
105113
AuthNamespace: namespace,
106114
},

0 commit comments

Comments
 (0)