Skip to content

Commit

Permalink
Fixes for plugin optional args (#85)
Browse files Browse the repository at this point in the history
1) Use struct pointer for KubernetesTransformPlugin funcs
2) Fix binary plugin marshal code to properly encode args
3) Use lower-cased arg names/keys
  • Loading branch information
sseago authored Nov 10, 2021
1 parent 72b0752 commit 675bd7c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 46 deletions.
19 changes: 11 additions & 8 deletions transform/binary-plugin/binary_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@ func (b *BinaryPlugin) Run(request transform.PluginRequest) (transform.PluginRes
p := transform.PluginResponse{}

out, errBytes, err := b.commandRunner.Run(request, b.log)
if err != nil {
b.log.Errorf("error running the plugin command")
return p, fmt.Errorf("error running the plugin command: %v", err)
}

if len(errBytes) != 0 {
logs := strings.Split(string(errBytes), "\n")
for _, line := range logs {
b.log.Debug("Plugin Log line: ", line)
}
}
if err != nil {
b.log.Errorf("error running the plugin command")
return p, fmt.Errorf("error running the plugin command: %v", err)
}


err = json.Unmarshal(out, &p)
if err != nil {
Expand Down Expand Up @@ -136,12 +136,15 @@ func (b *binaryRunner) Metadata(log logrus.FieldLogger) ([]byte, []byte, error)
}

func (b *binaryRunner) Run(request transform.PluginRequest, log logrus.FieldLogger) ([]byte, []byte, error) {
objJson, err := request.MarshalJSON()
unstructuredJson, err := request.MarshalJSON()
objMap := map[string]interface{}{}
json.Unmarshal(unstructuredJson, &objMap)
objMap["extras"] = request.Extras
objJson, err := json.Marshal(objMap)
if err != nil {
log.Errorf("unable to marshal unstructured Object")
return nil, nil, fmt.Errorf("unable to marshal unstructured Object: %s, err: %v", request, err)
}

command := cliContext.getCommand(b.pluginPath)

// set var to get the output
Expand All @@ -155,7 +158,7 @@ func (b *binaryRunner) Run(request transform.PluginRequest, log logrus.FieldLogg
err = command.Run()
if err != nil {
log.Errorf("unable to run the plugin binary")
return nil, nil, fmt.Errorf("unable to run the plugin binary, err: %v", err)
return nil, errorBytes.Bytes(), fmt.Errorf("unable to run the plugin binary, err: %v", err)
}
return out.Bytes(), errorBytes.Bytes(), nil
}
27 changes: 17 additions & 10 deletions transform/binary-plugin/examples/openshift/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,29 @@ import (

var logger logrus.FieldLogger

const (
// flags
StripDefaultPullSecrets = "strip-default-pull-secrets"
PullSecretReplacement = "pull-secret-replacement"
RegistryReplacement = "registry-replacement"
)

func main() {
logger = logrus.New()
// TODO: add plumbing for logger in the cli-library and instantiate here
fields := []transform.OptionalFields{
{
FlagName: "StripDefaultPullSecrets",
Help: "Whether to strip Pod and BuildConfig default pull secrets (beginning with builder/default/deployer-dockercfg-) that aren't replaced by the map param PullSecretReplacement",
FlagName: StripDefaultPullSecrets,
Help: "Whether to strip Pod and BuildConfig default pull secrets (beginning with builder/default/deployer-dockercfg-) that aren't replaced by the map param " + PullSecretReplacement,
Example: "true",
},
{
FlagName: "PullSecretReplacement",
FlagName: PullSecretReplacement,
Help: "Map of pull secrets to replace in Pods and BuildConfigs while transforming in format secret1=destsecret1,secret2=destsecret2[...]",
Example: "default-dockercfg-h4n7g=default-dockercfg-12345,builder-dockercfg-abcde=builder-dockercfg-12345",
},
{
FlagName: "RegistryReplacement",
FlagName: RegistryReplacement,
Help: "Map of image registry paths to swap on transform, in the format original-registry1=target-registry1,original-registry2=target-registry2...",
Example: "docker-registry.default.svc:5000=image-registry.openshift-image-registry.svc:5000,docker.io/foo=quay.io/bar",
},
Expand All @@ -43,17 +50,17 @@ type openshiftOptionalFields struct {
func getOptionalFields(extras map[string]string) (openshiftOptionalFields, error) {
var fields openshiftOptionalFields
var err error
if len(extras["StripDefaultPullSecrets"]) > 0 {
fields.StripDefaultPullSecrets, err = strconv.ParseBool(extras["StripDefaultPullSecrets"])
if len(extras[StripDefaultPullSecrets]) > 0 {
fields.StripDefaultPullSecrets, err = strconv.ParseBool(extras[StripDefaultPullSecrets])
if err != nil {
return fields, err
}
}
if len(extras["PullSecretReplacement"]) > 0 {
fields.PullSecretReplacement = transform.ParseOptionalFieldMapVal(extras["PullSecretReplacement"])
if len(extras[PullSecretReplacement]) > 0 {
fields.PullSecretReplacement = transform.ParseOptionalFieldMapVal(extras[PullSecretReplacement])
}
if len(extras["RegistryReplacement"]) > 0 {
fields.RegistryReplacement = transform.ParseOptionalFieldMapVal(extras["RegistryReplacement"])
if len(extras[RegistryReplacement]) > 0 {
fields.RegistryReplacement = transform.ParseOptionalFieldMapVal(extras[RegistryReplacement])
}
return fields, nil
}
Expand Down
70 changes: 42 additions & 28 deletions transform/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,26 @@ import (
transform "github.com/konveyor/crane-lib/transform"
"github.com/konveyor/crane-lib/transform/util"
"github.com/konveyor/crane-lib/transform/types"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
)

var logger logrus.FieldLogger

const (

// flags
NewNamespace = "new-namespace"
AddAnnotations = "add-annotations"
RemoveAnnotations = "remove-annotations"
RegistryReplacement = "registry-replacement"
ExtraWhiteouts = "extra-whiteouts"
IncludeOnly = "include-only"
DisableWhiteoutOwned = "disable-whiteout-owned"

containerImageUpdate = "/spec/template/spec/containers/%v/image"
initContainerImageUpdate = "/spec/template/spec/initContainers/%v/image"
podContainerImageUpdate = "/spec/containers/%v/image"
Expand Down Expand Up @@ -99,33 +112,34 @@ type KubernetesTransformPlugin struct {
IncludeOnly []schema.GroupKind
}

func (k KubernetesTransformPlugin) setOptionalFields(extras map[string]string) {
k.NewNamespace = extras["NewNamespace"]
if len(extras["AddAnnotations"]) > 0 {
k.AddAnnotations = transform.ParseOptionalFieldMapVal(extras["AddAnnotations"])
func (k *KubernetesTransformPlugin) setOptionalFields(extras map[string]string) {
k.NewNamespace = extras[NewNamespace]
if len(extras[AddAnnotations]) > 0 {
k.AddAnnotations = transform.ParseOptionalFieldMapVal(extras[AddAnnotations])
}
if len(extras["RemoveAnnotations"]) > 0 {
k.RemoveAnnotations = transform.ParseOptionalFieldSliceVal(extras["RemoveAnnotations"])
if len(extras[RemoveAnnotations]) > 0 {
k.RemoveAnnotations = transform.ParseOptionalFieldSliceVal(extras[RemoveAnnotations])
}
if len(extras["RegistryReplacement"]) > 0 {
k.RegistryReplacement = transform.ParseOptionalFieldMapVal(extras["RegistryReplacement"])
if len(extras[RegistryReplacement]) > 0 {
k.RegistryReplacement = transform.ParseOptionalFieldMapVal(extras[RegistryReplacement])
}
if len(extras["ExtraWhiteouts"]) > 0 {
k.ExtraWhiteouts = parseGroupKindSlice(transform.ParseOptionalFieldSliceVal(extras["ExtraWhiteouts"]))
if len(extras[ExtraWhiteouts]) > 0 {
k.ExtraWhiteouts = parseGroupKindSlice(transform.ParseOptionalFieldSliceVal(extras[ExtraWhiteouts]))
}
if len(extras["IncludeOnly"]) > 0 {
k.IncludeOnly = parseGroupKindSlice(transform.ParseOptionalFieldSliceVal(extras["IncludeOnly"]))
if len(extras[IncludeOnly]) > 0 {
k.IncludeOnly = parseGroupKindSlice(transform.ParseOptionalFieldSliceVal(extras[IncludeOnly]))
}
if len(extras["DisableWhiteoutOwned"]) > 0 {
if len(extras[DisableWhiteoutOwned]) > 0 {
var err error
k.DisableWhiteoutOwned, err = strconv.ParseBool(extras["DisableWhiteoutOwned"])
k.DisableWhiteoutOwned, err = strconv.ParseBool(extras[DisableWhiteoutOwned])
if err != nil {
k.DisableWhiteoutOwned = false
}
}
}

func (k KubernetesTransformPlugin) Run(request transform.PluginRequest) (transform.PluginResponse, error) {
func (k *KubernetesTransformPlugin) Run(request transform.PluginRequest) (transform.PluginResponse, error) {
logger = logrus.New()
k.setOptionalFields(request.Extras)
resp := transform.PluginResponse{}
// Set version in the future
Expand All @@ -140,46 +154,46 @@ func (k KubernetesTransformPlugin) Run(request transform.PluginRequest) (transfo

}

func (k KubernetesTransformPlugin) Metadata() transform.PluginMetadata {
func (k *KubernetesTransformPlugin) Metadata() transform.PluginMetadata {
return transform.PluginMetadata{
Name: "KubernetesPlugin",
Version: "v1",
RequestVersion: []transform.Version{transform.V1},
ResponseVersion: []transform.Version{transform.V1},
OptionalFields: []transform.OptionalFields{
{
FlagName: "AddAnnotations",
FlagName: AddAnnotations,
Help: "Annotations to add to each resource",
Example: "annotation1=value1,annotation2=value2",
},
{
FlagName: "RegistryReplacement",
FlagName: RegistryReplacement,
Help: "Map of image registry paths to swap on transform, in the format original-registry1=target-registry1,original-registry2=target-registry2...",
Example: "docker-registry.default.svc:5000=image-registry.openshift-image-registry.svc:5000,docker.io/foo=quay.io/bar",
},
{
FlagName: "NewNamespace",
Help: "Change the resource namespace to NewNamespace",
FlagName: NewNamespace,
Help: "Change the resource namespace to " + NewNamespace,
Example: "destination-namespace",
},
{
FlagName: "RemoveAnnotations",
FlagName: RemoveAnnotations,
Help: "Annotations to remove",
Example: "annotation1,annotation2",
},
{
FlagName: "DisableWhiteoutOwned",
FlagName: DisableWhiteoutOwned,
Help: "Disable whiting out owned pods and pod template resources",
Example: "true",
},
{
FlagName: "ExtraWhiteouts",
Help: "Additional resources to whiteout specified as a comma-separatedlist of GroupKind strings.",
FlagName: ExtraWhiteouts,
Help: "Additional resources to whiteout specified as a comma-separated list of GroupKind strings.",
Example: "Deployment.apps,Service,Route.route.openshift.io",
},
{
FlagName: "IncludeOnly",
Help: "If specified, every resource not listed here will be a whiteout. ExtraWhiteouts is ignored when IncludeONly is specified. Specified as a comma-separatedlist of GroupKind strings.",
FlagName: IncludeOnly,
Help: "If specified, every resource not listed here will be a whiteout. extra-whiteouts is ignored when include-only is specified. Specified as a comma-separated list of GroupKind strings.",
Example: "Deployment.apps,Service,Route.route.openshift.io",
},
},
Expand All @@ -188,7 +202,7 @@ func (k KubernetesTransformPlugin) Metadata() transform.PluginMetadata {

var _ transform.Plugin = &KubernetesTransformPlugin{}

func (k KubernetesTransformPlugin) getWhiteOuts(obj unstructured.Unstructured) bool {
func (k *KubernetesTransformPlugin) getWhiteOuts(obj unstructured.Unstructured) bool {
groupKind := obj.GroupVersionKind().GroupKind()
if len(k.IncludeOnly) > 0 {
if !groupKindInList(groupKind, k.IncludeOnly) {
Expand Down Expand Up @@ -229,7 +243,7 @@ func groupKindInList(gk schema.GroupKind, list []schema.GroupKind) bool {
return false
}

func (k KubernetesTransformPlugin) getKubernetesTransforms(obj unstructured.Unstructured) (jsonpatch.Patch, error) {
func (k *KubernetesTransformPlugin) getKubernetesTransforms(obj unstructured.Unstructured) (jsonpatch.Patch, error) {

// Always attempt to add annotations for each thing.
jsonPatch := jsonpatch.Patch{}
Expand Down

0 comments on commit 675bd7c

Please sign in to comment.