Skip to content

Commit

Permalink
Merge pull request #59 from jakalkhalili/v0.2.0
Browse files Browse the repository at this point in the history
#28 Enable groovy scripts secret loader
  • Loading branch information
tomaszsek authored Jul 18, 2019
2 parents 51f1030 + 52cd17e commit fd1d6ad
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
5 changes: 1 addition & 4 deletions pkg/controller/jenkins/configuration/user/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ func (r *ReconcileUserConfiguration) ensureUserConfiguration(jenkinsClient jenki
}
requeue, err = groovyClient.Ensure(func(name string) bool {
return strings.HasSuffix(name, ".groovy")
}, func(groovyScript string) string {
// TODO load secrets to variables
return groovyScript
})
}, groovy.AddSecretsLoaderToGroovyScript(resources.GroovyScriptsSecretVolumePath))
if err != nil {
return reconcile.Result{}, err
}
Expand Down
38 changes: 25 additions & 13 deletions test/e2e/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import (
"testing"

"github.com/jenkinsci/kubernetes-operator/pkg/apis/jenkins/v1alpha2"
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/groovy"
jenkinsclient "github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/client"
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/configuration/base"
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/configuration/base/resources"
"github.com/jenkinsci/kubernetes-operator/pkg/controller/jenkins/plugins"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/bndr/gojenkins"
"k8s.io/apimachinery/pkg/api/resource"
framework "github.com/operator-framework/operator-sdk/pkg/test"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestConfiguration(t *testing.T) {
Expand All @@ -27,6 +28,7 @@ func TestConfiguration(t *testing.T) {

jenkinsCRName := "e2e"
numberOfExecutors := 6
numberOfExecutorsEnvName := "NUMBER_OF_EXECUTORS"
systemMessage := "Configuration as Code integration works!!!"
systemMessageEnvName := "SYSTEM_MESSAGE"
mySeedJob := seedJobConfig{
Expand Down Expand Up @@ -66,14 +68,17 @@ func TestConfiguration(t *testing.T) {
},
}

stringData := make(map[string]string)
stringData[systemMessageEnvName] = systemMessage
stringData[numberOfExecutorsEnvName] = fmt.Sprintf("%d", numberOfExecutors)

// base
createUserConfigurationSecret(t, namespace, systemMessageEnvName, systemMessage)
createUserConfigurationConfigMap(t, namespace, numberOfExecutors, fmt.Sprintf("${%s}", systemMessageEnvName))
createUserConfigurationSecret(t, namespace, stringData)
createUserConfigurationConfigMap(t, namespace, numberOfExecutorsEnvName, fmt.Sprintf("${%s}", systemMessageEnvName))
jenkins := createJenkinsCR(t, jenkinsCRName, namespace, &[]v1alpha2.SeedJob{mySeedJob.SeedJob}, groovyScripts, casc)
createDefaultLimitsForContainersInNamespace(t, namespace)
createKubernetesCredentialsProviderSecret(t, namespace, mySeedJob)
waitForJenkinsBaseConfigurationToComplete(t, jenkins)

verifyJenkinsMasterPodAttributes(t, jenkins)
client := verifyJenkinsAPIConnection(t, jenkins)
verifyPlugins(t, client, jenkins)
Expand All @@ -84,15 +89,13 @@ func TestConfiguration(t *testing.T) {
verifyJenkinsSeedJobs(t, client, []seedJobConfig{mySeedJob})
}

func createUserConfigurationSecret(t *testing.T, namespace string, systemMessageEnvName, systemMessage string) {
func createUserConfigurationSecret(t *testing.T, namespace string, stringData map[string]string) {
userConfiguration := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: userConfigurationSecretName,
Namespace: namespace,
},
StringData: map[string]string{
systemMessageEnvName: systemMessage,
},
StringData: stringData,
}

t.Logf("User configuration secret %+v", *userConfiguration)
Expand All @@ -101,7 +104,7 @@ func createUserConfigurationSecret(t *testing.T, namespace string, systemMessage
}
}

func createUserConfigurationConfigMap(t *testing.T, namespace string, numberOfExecutors int, systemMessage string) {
func createUserConfigurationConfigMap(t *testing.T, namespace string, numberOfExecutorsSecretKeyName string, systemMessage string) {
userConfiguration := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: userConfigurationConfigMapName,
Expand All @@ -111,8 +114,8 @@ func createUserConfigurationConfigMap(t *testing.T, namespace string, numberOfEx
"1-set-executors.groovy": fmt.Sprintf(`
import jenkins.model.Jenkins
Jenkins.instance.setNumExecutors(%d)
Jenkins.instance.save()`, numberOfExecutors),
Jenkins.instance.setNumExecutors(new Integer(secrets['%s']))
Jenkins.instance.save()`, numberOfExecutorsSecretKeyName),
"1-casc.yaml": fmt.Sprintf(`
jenkins:
systemMessage: "%s"`, systemMessage),
Expand Down Expand Up @@ -275,6 +278,15 @@ if (!new Integer(%d).equals(Jenkins.instance.numExecutors)) {
logs, err := jenkinsClient.ExecuteScript(checkConfigurationViaGroovyScript)
assert.NoError(t, err, logs)

checkSecretLoaderViaGroovyScript := fmt.Sprintf(`
if (!new Integer(%d).equals(new Integer(secrets['NUMBER_OF_EXECUTORS']))) {
throw new Exception("Secret not found by given key: NUMBER_OF_EXECUTORS")
}`, amountOfExecutors)

loader := groovy.AddSecretsLoaderToGroovyScript("/var/jenkins/groovy-scripts-secrets")
logs, err = jenkinsClient.ExecuteScript(loader(checkSecretLoaderViaGroovyScript))
assert.NoError(t, err, logs)

checkConfigurationAsCode := fmt.Sprintf(`
if (!"%s".equals(Jenkins.instance.systemMessage)) {
throw new Exception("Configuration as code failed")
Expand Down

0 comments on commit fd1d6ad

Please sign in to comment.