2019-05-14 01:17:28 +00:00
|
|
|
package engine
|
2019-05-13 18:27:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
kubepolicy "github.com/nirmata/kube-policy/pkg/apis/policy/v1alpha1"
|
2019-05-14 01:17:28 +00:00
|
|
|
"github.com/nirmata/kube-policy/pkg/engine/mutation"
|
2019-05-13 18:27:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: To be reworked due to spec policy-v2
|
|
|
|
|
|
|
|
// Applies "configMapGenerator" and "secretGenerator" described in PolicyRule
|
2019-05-14 18:24:40 +00:00
|
|
|
func applyRuleGenerators(rawResource []byte, rule kubepolicy.Rule) error {
|
2019-05-13 18:27:47 +00:00
|
|
|
kind := mutation.ParseKindFromObject(rawResource)
|
|
|
|
|
|
|
|
// configMapGenerator and secretGenerator can be applied only to namespaces
|
|
|
|
if kind == "Namespace" {
|
|
|
|
namespaceName := mutation.ParseNameFromObject(rawResource)
|
|
|
|
|
2019-05-14 18:24:40 +00:00
|
|
|
err := applyConfigGenerator(rule.Generation, namespaceName, "ConfigMap")
|
2019-05-13 18:27:47 +00:00
|
|
|
if err == nil {
|
2019-05-14 18:24:40 +00:00
|
|
|
err = applyConfigGenerator(rule.Generation, namespaceName, "Secret")
|
2019-05-13 18:27:47 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates resourceKind (ConfigMap or Secret) with parameters specified in generator in cluster specified in request.
|
2019-05-14 18:24:40 +00:00
|
|
|
func applyConfigGenerator(generator *kubepolicy.Generation, namespace string, configKind string) error {
|
2019-05-13 18:27:47 +00:00
|
|
|
if generator == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := generator.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Generator for '%s' is invalid: %s", configKind, err)
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:24:40 +00:00
|
|
|
// TODO:
|
2019-05-13 18:27:47 +00:00
|
|
|
switch configKind {
|
|
|
|
case "ConfigMap":
|
2019-05-14 18:24:40 +00:00
|
|
|
// err = p.kubeClient.GenerateConfigMap(*generator, namespace)
|
2019-05-13 18:27:47 +00:00
|
|
|
case "Secret":
|
2019-05-14 18:24:40 +00:00
|
|
|
// err = p.kubeClient.GenerateSecret(*generator, namespace)
|
2019-05-13 18:27:47 +00:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("Unsupported config Kind '%s'", configKind)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to apply generator for %s '%s/%s' : %s", configKind, namespace, generator.Name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|