Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/cmd/operator-sdk/olm/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func newInstallCmd() *cobra.Command {
}

cmd.Flags().StringVar(&mgr.Version, "version", installer.DefaultVersion, "version of OLM resources to install")
cmd.Flags().StringSliceVar(&mgr.IgnoreKinds, "ignore-kinds", nil, "list of resource kinds to ignore during installation (e.g., \"Deployment,Service\")")
mgr.AddToFlagSet(cmd.Flags())
return cmd
}
27 changes: 25 additions & 2 deletions internal/olm/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,25 @@ func init() {
}

type Client struct {
KubeClient client.Client
KubeClient client.Client
ignoredKindsMap map[string]struct{}
}

func NewClientForConfig(cfg *rest.Config, httpClient *http.Client) (*Client, error) {
// Option is a function that configures a Client.
type Option func(*Client)

// WithIgnoredKinds returns an Option that sets the ignored resource kinds
func WithIgnoredKinds(ignoredKinds []string) Option {
return func(c *Client) {
// Create a map for O(1) lookup of ignored kinds
c.ignoredKindsMap = make(map[string]struct{})
for _, kind := range ignoredKinds {
c.ignoredKindsMap[kind] = struct{}{}
}
}
}

func NewClientForConfig(cfg *rest.Config, httpClient *http.Client, opts ...Option) (*Client, error) {
rm, err := apiutil.NewDynamicRESTMapper(cfg, httpClient)
if err != nil {
return nil, fmt.Errorf("failed to create dynamic rest mapper: %v", err)
Expand All @@ -115,6 +130,9 @@ func NewClientForConfig(cfg *rest.Config, httpClient *http.Client) (*Client, err
c := &Client{
KubeClient: cl,
}
for _, opt := range opts {
opt(c)
}
return c, nil
}

Expand All @@ -123,6 +141,11 @@ func (c Client) DoCreate(ctx context.Context, objs ...client.Object) error {
kind := obj.GetObjectKind().GroupVersionKind().Kind
resourceName := getName(obj.GetNamespace(), obj.GetName())

if _, ignored := c.ignoredKindsMap[kind]; ignored {
log.Infof(" Skipping %s %q (kind ignored)", kind, resourceName)
continue
}

log.Infof(" Creating %s %q", kind, resourceName)

if err := c.safeCreateOneResource(ctx, obj, kind, resourceName); err != nil {
Expand Down
13 changes: 11 additions & 2 deletions internal/olm/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,26 +273,35 @@ var _ = Describe("Client", func() {
fakeClient.(*errClient).reset()
})

It("should create all the resources successfully", func() {
cli := Client{KubeClient: fakeClient}
It("should create all the resources successfully, unless ignored", func() {
cli := Client{KubeClient: fakeClient, ignoredKindsMap: map[string]struct{}{"Service": {}}}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

Expect(cli.DoCreate(ctx,
&corev1.Namespace{
TypeMeta: metav1.TypeMeta{Kind: "Namespace"},
ObjectMeta: metav1.ObjectMeta{Name: "test-ns"},
},
&corev1.Pod{
TypeMeta: metav1.TypeMeta{Kind: "Pod"},
ObjectMeta: metav1.ObjectMeta{Name: "test-pod", Namespace: "test-ns"},
},
&corev1.Service{
TypeMeta: metav1.TypeMeta{Kind: "Service"},
ObjectMeta: metav1.ObjectMeta{Name: "ignored", Namespace: "test-ns"},
},
)).To(Succeed())

ns := &corev1.Namespace{}
Expect(fakeClient.Get(context.Background(), client.ObjectKey{Name: "test-ns"}, ns)).To(Succeed())

pod := &corev1.Pod{}
Expect(fakeClient.Get(context.Background(), client.ObjectKey{Namespace: "test-ns", Name: "test-pod"}, pod)).To(Succeed())

svc := &corev1.Service{}
Expect(fakeClient.Get(context.Background(), client.ObjectKey{Namespace: "test-ns", Name: "ignored"}, svc)).ToNot(Succeed())
})

It("should eventually create all the resources successfully", func() {
Expand Down
4 changes: 2 additions & 2 deletions internal/olm/installer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ type Client struct {
BaseDownloadURL string
}

func ClientForConfig(cfg *rest.Config) (*Client, error) {
func ClientForConfig(cfg *rest.Config, opts ...olmresourceclient.Option) (*Client, error) {
httpClient, err := rest.HTTPClientFor(cfg)
if err != nil {
return nil, fmt.Errorf("failed to build an HTTP client for the kubeconfig: %v", err)
}

cl, err := olmresourceclient.NewClientForConfig(cfg, httpClient)
cl, err := olmresourceclient.NewClientForConfig(cfg, httpClient, opts...)
if err != nil {
return nil, fmt.Errorf("failed to get OLM resource client: %v", err)
}
Expand Down
4 changes: 3 additions & 1 deletion internal/olm/installer/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"sync"
"time"

olmresourceclient "github.com/operator-framework/operator-sdk/internal/olm/client"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"sigs.k8s.io/controller-runtime/pkg/client/config"
Expand All @@ -39,6 +40,7 @@ type Manager struct {
Version string
Timeout time.Duration
OLMNamespace string
IgnoreKinds []string
once sync.Once
}

Expand All @@ -51,7 +53,7 @@ func (m *Manager) initialize() (err error) {
return
}

client, cerr := ClientForConfig(cfg)
client, cerr := ClientForConfig(cfg, olmresourceclient.WithIgnoredKinds(m.IgnoreKinds))
if cerr != nil {
err = fmt.Errorf("failed to create manager client: %v", cerr)
return
Expand Down
Loading