This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from moadqassem/validate-secrets
validating secrets on machine server calls
- Loading branch information
Showing
4 changed files
with
70 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package kubevirt | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
api "github.com/gardener/machine-controller-manager-provider-kubevirt/pkg/kubevirt/apis" | ||
clouderrors "github.com/gardener/machine-controller-manager-provider-kubevirt/pkg/kubevirt/errors" | ||
"github.com/gardener/machine-controller-manager-provider-kubevirt/pkg/kubevirt/validation" | ||
|
||
"github.com/gardener/machine-controller-manager/pkg/apis/machine/v1alpha1" | ||
"github.com/gardener/machine-controller-manager/pkg/util/provider/machinecodes/codes" | ||
"github.com/gardener/machine-controller-manager/pkg/util/provider/machinecodes/status" | ||
"github.com/pkg/errors" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/klog" | ||
) | ||
|
||
// decodeProviderSpecAndSecret converts request parameters to api.ProviderSpec | ||
func decodeProviderSpecAndSecret(machineClass *v1alpha1.MachineClass, secret *corev1.Secret) (*api.KubeVirtProviderSpec, error) { | ||
var ( | ||
providerSpec *api.KubeVirtProviderSpec | ||
) | ||
|
||
// Extract providerSpec | ||
err := json.Unmarshal(machineClass.ProviderSpec.Raw, &providerSpec) | ||
if err != nil { | ||
return nil, status.Error(codes.Internal, err.Error()) | ||
} | ||
|
||
validationErrors := validation.ValidateKubevirtProviderSpecAndSecret(providerSpec, secret) | ||
if validationErrors != nil { | ||
err = fmt.Errorf("error while validating ProviderSpec %v", validationErrors) | ||
return nil, status.Error(codes.Internal, err.Error()) | ||
} | ||
|
||
return providerSpec, nil | ||
} | ||
|
||
// prepareErrorf preapre, format and wrap an error on the machine server level. | ||
func prepareErrorf(err error, format string, args ...interface{}) error { | ||
var ( | ||
code codes.Code | ||
wrapped error | ||
) | ||
switch err.(type) { | ||
case *clouderrors.MachineNotFoundError: | ||
code = codes.NotFound | ||
wrapped = err | ||
default: | ||
code = codes.Internal | ||
wrapped = errors.Wrap(err, fmt.Sprintf(format, args...)) | ||
} | ||
klog.V(2).Infof(wrapped.Error()) | ||
return status.Error(code, wrapped.Error()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters