Allow to propagate envs

This commit is contained in:
mudler
2022-11-14 23:52:21 +01:00
parent 1da47ac24f
commit daab27404c
5 changed files with 157 additions and 34 deletions

View File

@@ -29,13 +29,14 @@ type EntanglementSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file // Important: Run "make" to regenerate code after modifying this file
ServiceUUID string `json:"serviceUUID,omitempty"` ServiceUUID string `json:"serviceUUID,omitempty"`
ServiceRef *string `json:"serviceRef,omitempty"` ServiceRef *string `json:"serviceRef,omitempty"`
SecretRef *string `json:"secretRef,omitempty"` SecretRef *string `json:"secretRef,omitempty"`
Host string `json:"host,omitempty"` Host string `json:"host,omitempty"`
Port string `json:"port,omitempty"` Port string `json:"port,omitempty"`
HostNetwork bool `json:"hostNetwork,omitempty"` HostNetwork bool `json:"hostNetwork,omitempty"`
Inbound bool `json:"inbound,omitempty"` Inbound bool `json:"inbound,omitempty"`
Envs []v1.EnvVar `json:"env,omitempty"`
// +kubebuilder:validation:Optional // +kubebuilder:validation:Optional
ServiceSpec *v1.ServiceSpec `json:"serviceSpec,omitEmpty"` ServiceSpec *v1.ServiceSpec `json:"serviceSpec,omitEmpty"`
} }

View File

@@ -98,6 +98,13 @@ func (in *EntanglementSpec) DeepCopyInto(out *EntanglementSpec) {
*out = new(string) *out = new(string)
**out = **in **out = **in
} }
if in.Envs != nil {
in, out := &in.Envs, &out.Envs
*out = make([]v1.EnvVar, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
if in.ServiceSpec != nil { if in.ServiceSpec != nil {
in, out := &in.ServiceSpec, &out.ServiceSpec in, out := &in.ServiceSpec, &out.ServiceSpec
*out = new(v1.ServiceSpec) *out = new(v1.ServiceSpec)

View File

@@ -35,6 +35,110 @@ spec:
spec: spec:
description: EntanglementSpec defines the desired state of Entanglement description: EntanglementSpec defines the desired state of Entanglement
properties: properties:
env:
items:
description: EnvVar represents an environment variable present in
a Container.
properties:
name:
description: Name of the environment variable. Must be a C_IDENTIFIER.
type: string
value:
description: 'Variable references $(VAR_NAME) are expanded using
the previously defined environment variables in the container
and any service environment variables. If a variable cannot
be resolved, the reference in the input string will be unchanged.
Double $$ are reduced to a single $, which allows for escaping
the $(VAR_NAME) syntax: i.e. "$$(VAR_NAME)" will produce the
string literal "$(VAR_NAME)". Escaped references will never
be expanded, regardless of whether the variable exists or
not. Defaults to "".'
type: string
valueFrom:
description: Source for the environment variable's value. Cannot
be used if value is not empty.
properties:
configMapKeyRef:
description: Selects a key of a ConfigMap.
properties:
key:
description: The key to select.
type: string
name:
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
TODO: Add other useful fields. apiVersion, kind, uid?'
type: string
optional:
description: Specify whether the ConfigMap or its key
must be defined
type: boolean
required:
- key
type: object
fieldRef:
description: 'Selects a field of the pod: supports metadata.name,
metadata.namespace, `metadata.labels[''<KEY>'']`, `metadata.annotations[''<KEY>'']`,
spec.nodeName, spec.serviceAccountName, status.hostIP,
status.podIP, status.podIPs.'
properties:
apiVersion:
description: Version of the schema the FieldPath is
written in terms of, defaults to "v1".
type: string
fieldPath:
description: Path of the field to select in the specified
API version.
type: string
required:
- fieldPath
type: object
resourceFieldRef:
description: 'Selects a resource of the container: only
resources limits and requests (limits.cpu, limits.memory,
limits.ephemeral-storage, requests.cpu, requests.memory
and requests.ephemeral-storage) are currently supported.'
properties:
containerName:
description: 'Container name: required for volumes,
optional for env vars'
type: string
divisor:
anyOf:
- type: integer
- type: string
description: Specifies the output format of the exposed
resources, defaults to "1"
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
x-kubernetes-int-or-string: true
resource:
description: 'Required: resource to select'
type: string
required:
- resource
type: object
secretKeyRef:
description: Selects a key of a secret in the pod's namespace
properties:
key:
description: The key of the secret to select from. Must
be a valid secret key.
type: string
name:
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names
TODO: Add other useful fields. apiVersion, kind, uid?'
type: string
optional:
description: Specify whether the Secret or its key must
be defined
type: boolean
required:
- key
type: object
type: object
required:
- name
type: object
type: array
host: host:
type: string type: string
hostNetwork: hostNetwork:

View File

@@ -40,25 +40,26 @@ func (r *EntanglementReconciler) genDeployment(ent entanglev1alpha1.Entanglement
} }
} }
v := ent.Spec.Envs
v = append(v, v1.EnvVar{
Name: "EDGEVPNTOKEN",
ValueFrom: &v1.EnvVarSource{
SecretKeyRef: &v1.SecretKeySelector{
Key: "network_token",
LocalObjectReference: v1.LocalObjectReference{
Name: *ent.Spec.SecretRef,
},
},
},
})
expose := v1.Container{ expose := v1.Container{
ImagePullPolicy: v1.PullAlways, ImagePullPolicy: v1.PullAlways,
SecurityContext: &v1.SecurityContext{Privileged: &privileged}, SecurityContext: &v1.SecurityContext{Privileged: &privileged},
Name: "entanglement", Name: "entanglement",
Image: r.EntangleServiceImage, Image: r.EntangleServiceImage,
Env: []v1.EnvVar{ Env: v,
{ Command: []string{"/usr/bin/edgevpn"},
Name: "EDGEVPNTOKEN",
ValueFrom: &v1.EnvVarSource{
SecretKeyRef: &v1.SecretKeySelector{
Key: "network_token",
LocalObjectReference: v1.LocalObjectReference{
Name: *ent.Spec.SecretRef,
},
},
},
},
},
Command: []string{"/usr/bin/edgevpn"},
} }
cmd := "service-add" cmd := "service-add"

View File

@@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"strings"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -36,6 +37,7 @@ var (
EntanglementDirectionLabel = "entanglement.kairos.io/direction" EntanglementDirectionLabel = "entanglement.kairos.io/direction"
EntanglementPortLabel = "entanglement.kairos.io/target_port" EntanglementPortLabel = "entanglement.kairos.io/target_port"
EntanglementHostLabel = "entanglement.kairos.io/host" EntanglementHostLabel = "entanglement.kairos.io/host"
EnvPrefix = "entanglement.kairos.io/env."
) )
func (w *Webhook) SetupWebhookWithManager(mgr manager.Manager) error { func (w *Webhook) SetupWebhookWithManager(mgr manager.Manager) error {
@@ -70,6 +72,26 @@ func (w *Webhook) Mutate(ctx context.Context, request admission.Request, object
return admission.Allowed("") return admission.Allowed("")
} }
envs := []corev1.EnvVar{
{
Name: "EDGEVPNTOKEN",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
Key: "network_token",
LocalObjectReference: corev1.LocalObjectReference{
Name: entanglementName,
},
},
},
}}
for k, v := range info {
if strings.HasPrefix(k, EnvPrefix) {
env := strings.ReplaceAll(k, EnvPrefix, "")
envs = append(envs, corev1.EnvVar{Name: env, Value: v})
}
}
entanglementPort, exists := info[EntanglementPortLabel] entanglementPort, exists := info[EntanglementPortLabel]
if !exists { if !exists {
return admission.Allowed("") return admission.Allowed("")
@@ -110,19 +132,7 @@ func (w *Webhook) Mutate(ctx context.Context, request admission.Request, object
ImagePullPolicy: corev1.PullAlways, ImagePullPolicy: corev1.PullAlways,
Command: []string{"/usr/bin/edgevpn"}, Command: []string{"/usr/bin/edgevpn"},
Args: []string{cmd, entanglementService, fmt.Sprintf("%s:%s", host, entanglementPort), "--log-level", w.LogLevel}, Args: []string{cmd, entanglementService, fmt.Sprintf("%s:%s", host, entanglementPort), "--log-level", w.LogLevel},
Env: []corev1.EnvVar{ Env: envs,
{
Name: "EDGEVPNTOKEN",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
Key: "network_token",
LocalObjectReference: corev1.LocalObjectReference{
Name: entanglementName,
},
},
},
},
},
SecurityContext: &corev1.SecurityContext{Privileged: &privileged}, SecurityContext: &corev1.SecurityContext{Privileged: &privileged},
Name: "entanglement", Name: "entanglement",
Image: w.SidecarImage, Image: w.SidecarImage,