This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstracted out the processing on containers. So now this abstracted functions can also be called on list of containers in init-container not just container in pod.
- Loading branch information
Showing
3 changed files
with
107 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/kedgeproject/kedge/pkg/spec" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
"github.com/pkg/errors" | ||
api_v1 "k8s.io/client-go/pkg/api/v1" | ||
) | ||
|
||
func populateProbes(c spec.Container) (spec.Container, error) { | ||
// check if health and liveness given together | ||
if c.Health != nil && (c.ReadinessProbe != nil || c.LivenessProbe != nil) { | ||
return c, fmt.Errorf("cannot define field 'health' and " + | ||
"'livnessProbe' or 'readinessProbe' together") | ||
} | ||
if c.Health != nil { | ||
c.LivenessProbe = c.Health | ||
c.ReadinessProbe = c.Health | ||
} | ||
return c, nil | ||
} | ||
|
||
func searchConfigMap(cms []spec.ConfigMapMod, name string) (spec.ConfigMapMod, error) { | ||
for _, cm := range cms { | ||
if cm.Name == name { | ||
return cm, nil | ||
} | ||
} | ||
return spec.ConfigMapMod{}, fmt.Errorf("configMap %q not found", name) | ||
} | ||
|
||
func convertEnvFromToEnvs(envFrom []spec.EnvFromSource, cms []spec.ConfigMapMod) ([]api_v1.EnvVar, error) { | ||
var envs []api_v1.EnvVar | ||
|
||
// we will iterate on all envFroms | ||
for ei, e := range envFrom { | ||
cmName := e.ConfigMapRef.Name | ||
|
||
// see if the configMap name which is given actually exists | ||
cm, err := searchConfigMap(cms, cmName) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "envFrom[%d].configMapRef.name", ei) | ||
} | ||
// once that configMap is found extract all data from it and create a env out of it | ||
for k := range cm.Data { | ||
envs = append(envs, api_v1.EnvVar{ | ||
Name: k, | ||
ValueFrom: &api_v1.EnvVarSource{ | ||
ConfigMapKeyRef: &api_v1.ConfigMapKeySelector{ | ||
LocalObjectReference: api_v1.LocalObjectReference{ | ||
Name: cmName, | ||
}, | ||
Key: k, | ||
}, | ||
}, | ||
}) | ||
} | ||
} | ||
return envs, nil | ||
} | ||
|
||
func populateEnvFrom(c spec.Container, cms []spec.ConfigMapMod) (spec.Container, error) { | ||
// now do the env from | ||
envs, err := convertEnvFromToEnvs(c.EnvFrom, cms) | ||
if err != nil { | ||
return c, err | ||
} | ||
// we collect all the envs from configMap before | ||
// envs provided inside the container | ||
envs = append(envs, c.Env...) | ||
c.Env = envs | ||
return c, nil | ||
} | ||
|
||
func populateContainers(containers []spec.Container, cms []spec.ConfigMapMod) ([]api_v1.Container, error) { | ||
var cnts []api_v1.Container | ||
|
||
for cn, c := range containers { | ||
// process health field | ||
c, err := populateProbes(c) | ||
if err != nil { | ||
return cnts, errors.Wrapf(err, "error converting 'health' to 'probes', app.containers[%d]", cn) | ||
} | ||
|
||
// process envFrom field | ||
c, err = populateEnvFrom(c, cms) | ||
if err != nil { | ||
return cnts, fmt.Errorf("error converting 'envFrom' to 'envs', app.containers[%d].%s", cn, err.Error()) | ||
} | ||
|
||
cnts = append(cnts, c.Container) | ||
} | ||
|
||
b, _ := json.MarshalIndent(cnts, "", " ") | ||
log.Debugf("containers after populating health: %s", string(b)) | ||
return cnts, nil | ||
} |