Skip to content

Commit 46fded7

Browse files
committed
feat: add --ignore-kinds to "olm install" cmd
Signed-off-by: Marcin Owsiany <porridge@redhat.com>
1 parent d7979db commit 46fded7

5 files changed

Lines changed: 42 additions & 7 deletions

File tree

internal/cmd/operator-sdk/olm/install.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func newInstallCmd() *cobra.Command {
3535
}
3636

3737
cmd.Flags().StringVar(&mgr.Version, "version", installer.DefaultVersion, "version of OLM resources to install")
38+
cmd.Flags().StringSliceVar(&mgr.IgnoreKinds, "ignore-kinds", nil, "list of resource kinds to ignore during installation (e.g., \"Deployment,Service\")")
3839
mgr.AddToFlagSet(cmd.Flags())
3940
return cmd
4041
}

internal/olm/client/client.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,25 @@ func init() {
8787
}
8888

8989
type Client struct {
90-
KubeClient client.Client
90+
KubeClient client.Client
91+
ignoredKindsMap map[string]struct{}
9192
}
9293

93-
func NewClientForConfig(cfg *rest.Config, httpClient *http.Client) (*Client, error) {
94+
// Option is a function that configures a Client.
95+
type Option func(*Client)
96+
97+
// WithIgnoredKinds returns an Option that sets the ignored resource kinds
98+
func WithIgnoredKinds(ignoredKinds []string) Option {
99+
return func(c *Client) {
100+
// Create a map for O(1) lookup of ignored kinds
101+
c.ignoredKindsMap = make(map[string]struct{})
102+
for _, kind := range ignoredKinds {
103+
c.ignoredKindsMap[kind] = struct{}{}
104+
}
105+
}
106+
}
107+
108+
func NewClientForConfig(cfg *rest.Config, httpClient *http.Client, opts ...Option) (*Client, error) {
94109
rm, err := apiutil.NewDynamicRESTMapper(cfg, httpClient)
95110
if err != nil {
96111
return nil, fmt.Errorf("failed to create dynamic rest mapper: %v", err)
@@ -115,6 +130,9 @@ func NewClientForConfig(cfg *rest.Config, httpClient *http.Client) (*Client, err
115130
c := &Client{
116131
KubeClient: cl,
117132
}
133+
for _, opt := range opts {
134+
opt(c)
135+
}
118136
return c, nil
119137
}
120138

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

144+
if _, ignored := c.ignoredKindsMap[kind]; ignored {
145+
log.Infof(" Skipping %s %q (kind ignored)", kind, resourceName)
146+
continue
147+
}
148+
126149
log.Infof(" Creating %s %q", kind, resourceName)
127150

128151
if err := c.safeCreateOneResource(ctx, obj, kind, resourceName); err != nil {

internal/olm/client/client_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,26 +273,35 @@ var _ = Describe("Client", func() {
273273
fakeClient.(*errClient).reset()
274274
})
275275

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

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

282282
Expect(cli.DoCreate(ctx,
283283
&corev1.Namespace{
284+
TypeMeta: metav1.TypeMeta{Kind: "Namespace"},
284285
ObjectMeta: metav1.ObjectMeta{Name: "test-ns"},
285286
},
286287
&corev1.Pod{
288+
TypeMeta: metav1.TypeMeta{Kind: "Pod"},
287289
ObjectMeta: metav1.ObjectMeta{Name: "test-pod", Namespace: "test-ns"},
288290
},
291+
&corev1.Service{
292+
TypeMeta: metav1.TypeMeta{Kind: "Service"},
293+
ObjectMeta: metav1.ObjectMeta{Name: "ignored", Namespace: "test-ns"},
294+
},
289295
)).To(Succeed())
290296

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

294300
pod := &corev1.Pod{}
295301
Expect(fakeClient.Get(context.Background(), client.ObjectKey{Namespace: "test-ns", Name: "test-pod"}, pod)).To(Succeed())
302+
303+
svc := &corev1.Service{}
304+
Expect(fakeClient.Get(context.Background(), client.ObjectKey{Namespace: "test-ns", Name: "ignored"}, svc)).ToNot(Succeed())
296305
})
297306

298307
It("should eventually create all the resources successfully", func() {

internal/olm/installer/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ type Client struct {
5656
BaseDownloadURL string
5757
}
5858

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

65-
cl, err := olmresourceclient.NewClientForConfig(cfg, httpClient)
65+
cl, err := olmresourceclient.NewClientForConfig(cfg, httpClient, opts...)
6666
if err != nil {
6767
return nil, fmt.Errorf("failed to get OLM resource client: %v", err)
6868
}

internal/olm/installer/manager.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"sync"
2121
"time"
2222

23+
olmresourceclient "github.com/operator-framework/operator-sdk/internal/olm/client"
2324
log "github.com/sirupsen/logrus"
2425
"github.com/spf13/pflag"
2526
"sigs.k8s.io/controller-runtime/pkg/client/config"
@@ -39,6 +40,7 @@ type Manager struct {
3940
Version string
4041
Timeout time.Duration
4142
OLMNamespace string
43+
IgnoreKinds []string
4244
once sync.Once
4345
}
4446

@@ -51,7 +53,7 @@ func (m *Manager) initialize() (err error) {
5153
return
5254
}
5355

54-
client, cerr := ClientForConfig(cfg)
56+
client, cerr := ClientForConfig(cfg, olmresourceclient.WithIgnoredKinds(m.IgnoreKinds))
5557
if cerr != nil {
5658
err = fmt.Errorf("failed to create manager client: %v", cerr)
5759
return

0 commit comments

Comments
 (0)