forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): Make configuration mangement easier. Closes argoproj#2463…
… (argoproj#2464)
- Loading branch information
Showing
43 changed files
with
751 additions
and
1,018 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
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
File renamed without changes.
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,122 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
apiv1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/fields" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/watch" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/cache" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
type Controller interface { | ||
Run(stopCh <-chan struct{}, onChange func(config Config) error) | ||
Get() (Config, error) | ||
} | ||
|
||
type controller struct { | ||
namespace string | ||
// name of the config map | ||
configMap string | ||
kubeclientset kubernetes.Interface | ||
} | ||
|
||
func NewController(namespace, name string, kubeclientset kubernetes.Interface) Controller { | ||
log.WithField("name", name).Info("config map") | ||
return &controller{ | ||
namespace: namespace, | ||
configMap: name, | ||
kubeclientset: kubeclientset, | ||
} | ||
} | ||
|
||
func (cc *controller) updateConfig(cm *apiv1.ConfigMap, onChange func(config Config) error) error { | ||
c, err := parseConfigMap(cm) | ||
if err != nil { | ||
return err | ||
} | ||
return onChange(c) | ||
} | ||
|
||
func parseConfigMap(cm *apiv1.ConfigMap) (Config, error) { | ||
// The key in the configmap to retrieve workflow configuration from. | ||
// Content encoding is expected to be YAML. | ||
var c Config | ||
config, ok := cm.Data["config"] | ||
if ok && len(cm.Data) != 1 { | ||
return c, fmt.Errorf("if you have an item in your config map named 'config', you must only have one item") | ||
} | ||
if !ok { | ||
for name, value := range cm.Data { | ||
if strings.Contains(value, "\n") { | ||
// this mucky code indents with two spaces | ||
config = config + name + ":\n " + strings.Join(strings.Split(strings.Trim(value, "\n"), "\n"), "\n ") + "\n" | ||
} else { | ||
config = config + name + ": " + value + "\n" | ||
} | ||
} | ||
} | ||
return c, yaml.Unmarshal([]byte(config), &c) | ||
} | ||
|
||
func (cc *controller) Run(stopCh <-chan struct{}, onChange func(config Config) error) { | ||
restClient := cc.kubeclientset.CoreV1().RESTClient() | ||
resource := "configmaps" | ||
fieldSelector := fields.ParseSelectorOrDie(fmt.Sprintf("metadata.name=%s", cc.configMap)) | ||
listFunc := func(options metav1.ListOptions) (runtime.Object, error) { | ||
options.FieldSelector = fieldSelector.String() | ||
req := restClient.Get(). | ||
Namespace(cc.namespace). | ||
Resource(resource). | ||
VersionedParams(&options, metav1.ParameterCodec) | ||
return req.Do().Get() | ||
} | ||
watchFunc := func(options metav1.ListOptions) (watch.Interface, error) { | ||
options.Watch = true | ||
options.FieldSelector = fieldSelector.String() | ||
req := restClient.Get(). | ||
Namespace(cc.namespace). | ||
Resource(resource). | ||
VersionedParams(&options, metav1.ParameterCodec) | ||
return req.Watch() | ||
} | ||
source := &cache.ListWatch{ListFunc: listFunc, WatchFunc: watchFunc} | ||
_, controller := cache.NewInformer( | ||
source, | ||
&apiv1.ConfigMap{}, | ||
0, | ||
cache.ResourceEventHandlerFuncs{ | ||
UpdateFunc: func(old, new interface{}) { | ||
oldCM := old.(*apiv1.ConfigMap) | ||
newCM := new.(*apiv1.ConfigMap) | ||
if oldCM.ResourceVersion == newCM.ResourceVersion { | ||
return | ||
} | ||
if newCm, ok := new.(*apiv1.ConfigMap); ok { | ||
log.Infof("Detected ConfigMap update.") | ||
err := cc.updateConfig(newCm, onChange) | ||
if err != nil { | ||
log.Errorf("Update of config failed due to: %v", err) | ||
} | ||
} | ||
}, | ||
}) | ||
controller.Run(stopCh) | ||
log.Info("Watching config map updates") | ||
} | ||
|
||
func (cc *controller) Get() (Config, error) { | ||
cmClient := cc.kubeclientset.CoreV1().ConfigMaps(cc.namespace) | ||
cm, err := cmClient.Get(cc.configMap, metav1.GetOptions{}) | ||
if err != nil { | ||
return Config{}, err | ||
} | ||
return parseConfigMap(cm) | ||
} |
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,42 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
apiv1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func Test_parseConfigMap(t *testing.T) { | ||
t.Run("Empty", func(t *testing.T) { | ||
_, err := parseConfigMap(&apiv1.ConfigMap{}) | ||
assert.NoError(t, err) | ||
}) | ||
t.Run("Config", func(t *testing.T) { | ||
c, err := parseConfigMap(&apiv1.ConfigMap{Data: map[string]string{"config": "containerRuntimeExecutor: pns"}}) | ||
if assert.NoError(t, err) { | ||
assert.Equal(t, "pns", c.ContainerRuntimeExecutor) | ||
} | ||
}) | ||
t.Run("Complex", func(t *testing.T) { | ||
c, err := parseConfigMap(&apiv1.ConfigMap{Data: map[string]string{"containerRuntimeExecutor": "pns", "artifactRepository": ` archiveLogs: true | ||
s3: | ||
bucket: my-bucket | ||
endpoint: minio:9000 | ||
insecure: true | ||
accessKeySecret: | ||
name: my-minio-cred | ||
key: accesskey | ||
secretKeySecret: | ||
name: my-minio-cred | ||
key: secretkey`}}) | ||
if assert.NoError(t, err) { | ||
assert.Equal(t, "pns", c.ContainerRuntimeExecutor) | ||
assert.NotEmpty(t, c.ArtifactRepository) | ||
} | ||
}) | ||
t.Run("IgnoreGarbage", func(t *testing.T) { | ||
_, err := parseConfigMap(&apiv1.ConfigMap{Data: map[string]string{"garbage": "garbage"}}) | ||
assert.NoError(t, err) | ||
}) | ||
} |
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
Oops, something went wrong.