Add secrets to Density and Load tests

This commit is contained in:
gmarek
2016-12-01 16:43:27 +01:00
parent c3a2cc5370
commit be3889810d
3 changed files with 181 additions and 26 deletions

View File

@@ -111,6 +111,9 @@ type RCConfig struct {
// kubelets are running those variables should be nil.
NodeDumpFunc func(c clientset.Interface, nodeNames []string, logFunc func(fmt string, args ...interface{}))
ContainerDumpFunc func(c clientset.Interface, ns string, logFunc func(ftm string, args ...interface{}))
// Names of the secrets to mount
SecretNames []string
}
func (rc *RCConfig) RCConfigLog(fmt string, args ...interface{}) {
@@ -245,6 +248,10 @@ func (config *DeploymentConfig) create() error {
},
}
if len(config.SecretNames) > 0 {
attachSecrets(&deployment.Spec.Template, config.SecretNames)
}
config.applyTo(&deployment.Spec.Template)
_, err := config.Client.Extensions().Deployments(config.Namespace).Create(deployment)
@@ -305,6 +312,10 @@ func (config *ReplicaSetConfig) create() error {
},
}
if len(config.SecretNames) > 0 {
attachSecrets(&rs.Spec.Template, config.SecretNames)
}
config.applyTo(&rs.Spec.Template)
_, err := config.Client.Extensions().ReplicaSets(config.Namespace).Create(rs)
@@ -398,6 +409,10 @@ func (config *RCConfig) create() error {
},
}
if len(config.SecretNames) > 0 {
attachSecrets(rc.Spec.Template, config.SecretNames)
}
config.applyTo(rc.Spec.Template)
_, err := config.Client.Core().ReplicationControllers(config.Namespace).Create(rc)
@@ -926,3 +941,62 @@ func NewSimpleWithControllerCreatePodStrategy(controllerName string) TestPodCrea
return createPod(client, namespace, podCount, basePod)
}
}
type SecretConfig struct {
Content map[string]string
Client clientset.Interface
Name string
Namespace string
// If set this function will be used to print log lines instead of glog.
LogFunc func(fmt string, args ...interface{})
}
func (config *SecretConfig) Run() error {
secret := &v1.Secret{
ObjectMeta: v1.ObjectMeta{
Name: config.Name,
},
StringData: map[string]string{},
}
for k, v := range config.Content {
secret.StringData[k] = v
}
_, err := config.Client.Core().Secrets(config.Namespace).Create(secret)
if err != nil {
return fmt.Errorf("Error creating secret: %v", err)
}
config.LogFunc("Created secret %v/%v", config.Namespace, config.Name)
return nil
}
func (config *SecretConfig) Stop() error {
if err := config.Client.Core().Secrets(config.Namespace).Delete(config.Name, &v1.DeleteOptions{}); err != nil {
return fmt.Errorf("Error deleting secret: %v", err)
}
config.LogFunc("Deleted secret %v/%v", config.Namespace, config.Name)
return nil
}
// TODO: attach secrets using different possibilities: env vars, image pull secrets.
func attachSecrets(template *v1.PodTemplateSpec, secretNames []string) {
volumes := make([]v1.Volume, 0, len(secretNames))
mounts := make([]v1.VolumeMount, 0, len(secretNames))
for _, name := range secretNames {
volumes = append(volumes, v1.Volume{
Name: name,
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: name,
},
},
})
mounts = append(mounts, v1.VolumeMount{
Name: name,
MountPath: fmt.Sprintf("/%v", name),
})
}
template.Spec.Volumes = volumes
template.Spec.Containers[0].VolumeMounts = mounts
}