Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ func newClient(config *rest.Config, options Options) (*client, error) {
mapper: options.Mapper,
codecs: serializer.NewCodecFactory(options.Scheme),

structuredResourceByType: make(map[schema.GroupVersionKind]*resourceMeta),
unstructuredResourceByType: make(map[schema.GroupVersionKind]*resourceMeta),
structuredResourceByType: make(map[cacheKey]*resourceMeta),
unstructuredResourceByType: make(map[cacheKey]*resourceMeta),
}

rawMetaClient, err := metadata.NewForConfigAndClient(metadata.ConfigFor(config), options.HTTPClient)
Expand Down
22 changes: 17 additions & 5 deletions pkg/client/client_rest_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,17 @@ type clientRestResources struct {
codecs serializer.CodecFactory

// structuredResourceByType stores structured type metadata
structuredResourceByType map[schema.GroupVersionKind]*resourceMeta
structuredResourceByType map[cacheKey]*resourceMeta

// unstructuredResourceByType stores unstructured type metadata
unstructuredResourceByType map[schema.GroupVersionKind]*resourceMeta
mu sync.RWMutex
unstructuredResourceByType map[cacheKey]*resourceMeta
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can tell the only reason for having these two was that unstructured had the same problem of never being able to use proto - Would you mind to de-duplicate them into a single resourceByType?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right. I have addressed this. Thank you!


mu sync.RWMutex
}

type cacheKey struct {
gvk schema.GroupVersionKind
forceDisableProtoBuf bool
}

// newResource maps obj to a Kubernetes Resource and constructs a client for that Resource.
Expand Down Expand Up @@ -117,11 +124,16 @@ func (c *clientRestResources) getResource(obj any) (*resourceMeta, error) {
// It's better to do creation work twice than to not let multiple
// people make requests at once
c.mu.RLock()

cacheKey := cacheKey{gvk: gvk, forceDisableProtoBuf: forceDisableProtoBuf}

resourceByType := c.structuredResourceByType
if isUnstructured {
resourceByType = c.unstructuredResourceByType
}
r, known := resourceByType[gvk]

r, known := resourceByType[cacheKey]

c.mu.RUnlock()

if known {
Expand All @@ -140,7 +152,7 @@ func (c *clientRestResources) getResource(obj any) (*resourceMeta, error) {
if err != nil {
return nil, err
}
resourceByType[gvk] = r
resourceByType[cacheKey] = r
return r, err
}

Expand Down
52 changes: 52 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,58 @@ U5wwSivyi7vmegHKmblOzNVKA5qPO8zWzqBC
Expect(cm.Data).To(BeComparableTo(data))
Expect(cm.Data).To(BeComparableTo(obj.Data))
})

It("should create a secret without SSA and later create update a secret using SSA", func(ctx SpecContext) {
cl, err := client.New(cfg, client.Options{})
Expect(err).NotTo(HaveOccurred())
Expect(cl).NotTo(BeNil())
data := map[string][]byte{
"some-key": []byte("some-value"),
}
secretObject := &corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't needed and can be removed

Suggested change
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: "v1",
},

ObjectMeta: metav1.ObjectMeta{
Name: "secret-one",
Namespace: "default",
},
Data: data,
}

secretApplyConfiguration := corev1applyconfigurations.
Secret("secret-two", "default").
WithAPIVersion("v1").
WithKind("Secret").
WithData(data)

err = cl.Create(ctx, secretObject)
Expect(err).NotTo(HaveOccurred())

err = cl.Apply(ctx, secretApplyConfiguration, &client.ApplyOptions{FieldManager: "test-manager"})
Expect(err).NotTo(HaveOccurred())

cm, err := clientset.CoreV1().Secrets(ptr.Deref(secretApplyConfiguration.GetNamespace(), "")).Get(ctx, ptr.Deref(secretApplyConfiguration.GetName(), ""), metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred())

Expect(cm.Data).To(BeComparableTo(data))
Expect(cm.Data).To(BeComparableTo(secretApplyConfiguration.Data))

data = map[string][]byte{
"some-key": []byte("some-new-value"),
}
secretApplyConfiguration.Data = data

err = cl.Apply(ctx, secretApplyConfiguration, &client.ApplyOptions{FieldManager: "test-manager"})
Expect(err).NotTo(HaveOccurred())

cm, err = clientset.CoreV1().Secrets(ptr.Deref(secretApplyConfiguration.GetNamespace(), "")).Get(ctx, ptr.Deref(secretApplyConfiguration.GetName(), ""), metav1.GetOptions{})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

Suggested change
cm, err = clientset.CoreV1().Secrets(ptr.Deref(secretApplyConfiguration.GetNamespace(), "")).Get(ctx, ptr.Deref(secretApplyConfiguration.GetName(), ""), metav1.GetOptions{})
secret, err = clientset.CoreV1().Secrets(ptr.Deref(secretApplyConfiguration.GetNamespace(), "")).Get(ctx, ptr.Deref(secretApplyConfiguration.GetName(), ""), metav1.GetOptions{})

Expect(err).NotTo(HaveOccurred())

Expect(cm.Data).To(BeComparableTo(data))
Expect(cm.Data).To(BeComparableTo(secretApplyConfiguration.Data))
})
})
})

Expand Down