Skip to content

Commit

Permalink
Fix NPE in addofflinelicense (#338)
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha authored Sep 30, 2024
1 parent 4faffbd commit 9014a93
Showing 1 changed file with 7 additions and 6 deletions.
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

0 comments on commit 9014a93

Please sign in to comment.