Add --plugins flag to velero install (#1930)

* Add plugins flag to install

Signed-off-by: Nolan Brubaker <brubakern@vmware.com>
This commit is contained in:
Nolan Brubaker
2019-10-03 19:44:52 -04:00
committed by KubeKween
parent db59d8d4bc
commit f009fe9bd1
7 changed files with 63 additions and 34 deletions

View File

@@ -64,6 +64,7 @@ type InstallOptions struct {
Wait bool
UseVolumeSnapshots bool
DefaultResticMaintenanceFrequency time.Duration
Plugins flag.StringArray
}
// BindFlags adds command line values to the options struct.
@@ -92,6 +93,7 @@ func (o *InstallOptions) BindFlags(flags *pflag.FlagSet) {
flags.BoolVar(&o.UseRestic, "use-restic", o.UseRestic, "create restic deployment. Optional.")
flags.BoolVar(&o.Wait, "wait", o.Wait, "wait for Velero deployment to be ready. Optional.")
flags.DurationVar(&o.DefaultResticMaintenanceFrequency, "default-restic-prune-frequency", o.DefaultResticMaintenanceFrequency, "how often 'restic prune' is run for restic repositories by default. Optional.")
flags.Var(&o.Plugins, "plugins", "Plugin container images to install into the Velero Deployment. Optional.")
}
// NewInstallOptions instantiates a new, default InstallOptions struct.
@@ -155,6 +157,7 @@ func (o *InstallOptions) AsVeleroOptions() (*install.VeleroOptions, error) {
BSLConfig: o.BackupStorageConfig.Data(),
VSLConfig: o.VolumeSnapshotConfig.Data(),
DefaultResticMaintenanceFrequency: o.DefaultResticMaintenanceFrequency,
Plugins: o.Plugins,
}, nil
}

View File

@@ -28,6 +28,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"github.com/vmware-tanzu/velero/pkg/builder"
"github.com/vmware-tanzu/velero/pkg/client"
"github.com/vmware-tanzu/velero/pkg/cmd"
"github.com/vmware-tanzu/velero/pkg/cmd/util/flag"
@@ -104,17 +105,7 @@ func NewAddCommand(f client.Factory) *cobra.Command {
}
// add the plugin as an init container
plugin := v1.Container{
Name: getName(args[0]),
Image: args[0],
ImagePullPolicy: v1.PullPolicy(imagePullPolicyFlag.String()),
VolumeMounts: []v1.VolumeMount{
{
Name: pluginsVolumeName,
MountPath: "/target",
},
},
}
plugin := *builder.ForPluginContainer(args[0], v1.PullPolicy(imagePullPolicyFlag.String())).Result()
veleroDeploy.Spec.Template.Spec.InitContainers = append(veleroDeploy.Spec.Template.Spec.InitContainers, plugin)
@@ -134,23 +125,3 @@ func NewAddCommand(f client.Factory) *cobra.Command {
return c
}
// getName returns the 'name' component of a docker
// image (i.e. everything after the last '/' and before
// any subsequent ':')
func getName(image string) string {
slashIndex := strings.LastIndex(image, "/")
colonIndex := strings.LastIndex(image, ":")
start := 0
if slashIndex > 0 {
start = slashIndex + 1
}
end := len(image)
if colonIndex > slashIndex {
end = colonIndex
}
return image[start:end]
}

View File

@@ -1,62 +0,0 @@
/*
Copyright 2018 the Velero contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package plugin
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetName(t *testing.T) {
tests := []struct {
name string
image string
expected string
}{
{
name: "image name with registry hostname and tag",
image: "gcr.io/my-repo/my-image:latest",
expected: "my-image",
},
{
name: "image name with registry hostname, without tag",
image: "gcr.io/my-repo/my-image",
expected: "my-image",
},
{
name: "image name without registry hostname, with tag",
image: "my-repo/my-image:latest",
expected: "my-image",
},
{
name: "image name without registry hostname, without tag",
image: "my-repo/my-image",
expected: "my-image",
},
{
name: "image name with registry hostname and port, and tag",
image: "mycustomregistry.io:8080/my-repo/my-image:latest",
expected: "my-image",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expected, getName(test.image))
})
}
}