Skip to content
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

Fix NPE in addofflinelicense #338

Merged
merged 1 commit into from
Sep 30, 2024
Merged
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
13 changes: 7 additions & 6 deletions pkg/registry/offline/addofflinelicense/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
licenseapi "kubeops.dev/ui-server/apis/offline/v1alpha1"

core "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -105,7 +104,7 @@ func (r *Storage) Create(ctx context.Context, obj runtime.Object, _ rest.Validat
return nil, apierrors.NewBadRequest("missing license info")
}

licenseSecret := v1.Secret{}
var licenseSecret core.Secret
err := r.kc.Get(ctx, types.NamespacedName{Name: LicenseSecretName, Namespace: req.Namespace}, &licenseSecret)
if err != nil && apierrors.IsNotFound(err) {
// check permission
Expand All @@ -131,7 +130,7 @@ func (r *Storage) Create(ctx context.Context, obj runtime.Object, _ rest.Validat
return nil, err
}

licenseSecret = v1.Secret{
licenseSecret = core.Secret{
ObjectMeta: controllerruntime.ObjectMeta{
Name: LicenseSecretName,
Namespace: req.Namespace,
Expand Down Expand Up @@ -179,11 +178,13 @@ func (r *Storage) Create(ctx context.Context, obj runtime.Object, _ rest.Validat
if err != nil {
return nil, err
}
licenseSecret.Data[productKey] = []byte(req.License)

_, err = cg.CreateOrPatch(ctx, r.kc, &licenseSecret, func(obj client.Object, createOp bool) client.Object {
in := obj.(*v1.Secret)
in.Data = licenseSecret.Data
in := obj.(*core.Secret)
if in.Data == nil {
in.Data = map[string][]byte{}
}
in.Data[productKey] = []byte(req.License)
return in
})
if err != nil {
Expand Down
Loading