Commit a5a19bdf authored by Kevin Di Lallo's avatar Kevin Di Lallo
Browse files

webhook updates to set pod, sandbox & scenario names as env var in scenario pods

parent e52450bd
Loading
Loading
Loading
Loading
+2 −7
Original line number Diff line number Diff line
@@ -300,13 +300,8 @@ func getNetworkLocation(name string) (zone string, netLoc string, err error) {
		err = errors.New("Error getting context for: " + name)
		return
	}
	nodeCtx, ok := ctx.(*mod.NodeContext)
	if !ok {
		err = errors.New("Error casting context for: " + name)
		return
	}
	zone = nodeCtx.Parents[mod.Zone]
	netLoc = nodeCtx.Parents[mod.NetLoc]
	zone = ctx.Parents[mod.Zone]
	netLoc = ctx.Parents[mod.NetLoc]
	return zone, netLoc, nil
}

+2 −7
Original line number Diff line number Diff line
@@ -400,18 +400,13 @@ func processScenario(model *mod.Model) error {
			err := errors.New("Error getting context for process: " + name)
			return err
		}
		nodeCtx, ok := ctx.(*mod.NodeContext)
		if !ok {
			err := errors.New("Error casting context for process: " + name)
			return err
		}

		// Get network element from list or create new one if it does not exist
		netElem := getNetElem(proc.Name)

		// Set current physical & network location and network locations in range
		netElem.phyLoc = nodeCtx.Parents[mod.PhyLoc]
		netElem.netLoc = nodeCtx.Parents[mod.NetLoc]
		netElem.phyLoc = ctx.Parents[mod.PhyLoc]
		netElem.netLoc = ctx.Parents[mod.NetLoc]
		phyLocNode := model.GetNode(netElem.phyLoc)
		if phyLocNode == nil {
			err := errors.New("Error finding physical location: " + netElem.phyLoc)
+41 −15
Original line number Diff line number Diff line
@@ -111,17 +111,10 @@ func isScenarioResource(name string, scenarioName string) bool {
	return name != "" && strings.HasPrefix(name, "meep-"+scenarioName+"-")
}

func getSidecarPatch(template corev1.PodTemplateSpec, sidecarConfig *Config, meepAppName string, sandboxName string) (patch []byte, err error) {

	// Apply labels
	newLabels := make(map[string]string)
	newLabels["meepApp"] = meepAppName
	newLabels["meepOrigin"] = meepOrigin
	newLabels["meepSandbox"] = sandboxName
	newLabels["meepScenario"] = activeScenarioNames[sandboxName]
	newLabels["processId"] = meepAppName
func getPlatformPatch(template corev1.PodTemplateSpec, sidecarConfig *Config, meepAppName string, sandboxName string) (patch []byte, err error) {
	var patchOps []patchOperation

	// Add environment variables to sidecar containers
	// Add env vars to sidecar containers
	var envVars []corev1.EnvVar
	var envVar corev1.EnvVar
	envVar.Name = "MEEP_POD_NAME"
@@ -140,13 +133,25 @@ func getSidecarPatch(template corev1.PodTemplateSpec, sidecarConfig *Config, mee
		sidecarContainers = append(sidecarContainers, container)
	}

	// Create patch operations
	var patchOps []patchOperation
	// Add env vars to scenario containers
	for idx, container := range template.Spec.Containers {
		patchOps = append(patchOps, addEnvVar(container.Env, envVars, fmt.Sprintf("/spec/template/spec/containers/%d/env", idx))...)
	}

	// Add sidecar containers
	patchOps = append(patchOps, addContainer(template.Spec.Containers, sidecarContainers, "/spec/template/spec/containers")...)
	patchOps = append(patchOps, addVolume(template.Spec.Volumes, sidecarConfig.Volumes, "/spec/template/spec/volumes")...)

	// Add labels
	newLabels := make(map[string]string)
	newLabels["meepApp"] = meepAppName
	newLabels["meepOrigin"] = meepOrigin
	newLabels["meepSandbox"] = sandboxName
	newLabels["meepScenario"] = activeScenarioNames[sandboxName]
	newLabels["processId"] = meepAppName
	patchOps = append(patchOps, updateLabels(template.ObjectMeta.Labels, newLabels, "/spec/template/metadata/labels")...)

	// Init Cointainer for dependency check
	// Init Container for dependency check
	var initContainers []corev1.Container
	initContainers = append(initContainers, sidecarConfig.InitContainers...)
	patchOps = append(patchOps, addContainer(template.Spec.InitContainers, initContainers, "/spec/template/spec/initContainers")...)
@@ -219,6 +224,27 @@ func updateLabels(target map[string]string, added map[string]string, basePath st
	return patch
}

func addEnvVar(target, added []corev1.EnvVar, basePath string) (patch []patchOperation) {
	first := len(target) == 0
	var value interface{}
	for _, add := range added {
		value = add
		path := basePath
		if first {
			first = false
			value = []corev1.EnvVar{add}
		} else {
			path = path + "/-"
		}
		patch = append(patch, patchOperation{
			Op:    "add",
			Path:  path,
			Value: value,
		})
	}
	return patch
}

// main mutation process
func (whsvr *WebhookServer) mutate(ar *v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
	req := ar.Request
@@ -285,8 +311,8 @@ func (whsvr *WebhookServer) mutate(ar *v1beta1.AdmissionReview) *v1beta1.Admissi
		}
	}

	// Get sidecar patch
	patch, err := getSidecarPatch(template, whsvr.sidecarConfig, resourceName, req.Namespace)
	// Get platform patch
	patch, err := getPlatformPatch(template, whsvr.sidecarConfig, resourceName, req.Namespace)
	if err != nil {
		return &v1beta1.AdmissionResponse{
			Result: &metav1.Status{
+7 −4
Original line number Diff line number Diff line
@@ -942,16 +942,19 @@ func (m *Model) GetNodeChild(name string) (child interface{}) {
}

// GetNodeContext - Get a node context
// 		Returned value is of type interface{}
//    Good practice: returned node should be type asserted with val,ok := node.(someType) to prevent panic
func (m *Model) GetNodeContext(name string) (ctx interface{}) {
func (m *Model) GetNodeContext(name string) (ctx *NodeContext) {
	m.lock.RLock()
	defer m.lock.RUnlock()

	ctx = nil
	n := m.nodeMap.nameMap[name]
	if n != nil {
		ctx = n.context
		nodeCtx := n.context
		var ok bool
		if ctx, ok = nodeCtx.(*NodeContext); !ok {
			log.Error("Error casting node context for node: " + name)
			return nil
		}
	}
	return ctx
}
+6 −12
Original line number Diff line number Diff line
@@ -1007,8 +1007,7 @@ func TestGetters(t *testing.T) {
	if ctx == nil {
		t.Fatalf("Node context should exist")
	}
	nodeCtx, ok := ctx.(*NodeContext)
	if !ok || !validateNodeContext(nodeCtx, "demo1", "", "", "", "") {
	if !validateNodeContext(ctx, "demo1", "", "", "", "") {
		t.Fatalf("Invalid Deployment context")
	}
	fmt.Println("Get Operator context")
@@ -1016,8 +1015,7 @@ func TestGetters(t *testing.T) {
	if ctx == nil {
		t.Fatalf("Node context should exist")
	}
	nodeCtx, ok = ctx.(*NodeContext)
	if !ok || !validateNodeContext(nodeCtx, "demo1", "operator1", "", "", "") {
	if !validateNodeContext(ctx, "demo1", "operator1", "", "", "") {
		t.Fatalf("Invalid Operator context")
	}
	fmt.Println("Get Zone context")
@@ -1025,8 +1023,7 @@ func TestGetters(t *testing.T) {
	if ctx == nil {
		t.Fatalf("Node context should exist")
	}
	nodeCtx, ok = ctx.(*NodeContext)
	if !ok || !validateNodeContext(nodeCtx, "demo1", "operator1", "zone1", "", "") {
	if !validateNodeContext(ctx, "demo1", "operator1", "zone1", "", "") {
		t.Fatalf("Invalid Operator context")
	}
	fmt.Println("Get Net Location context")
@@ -1034,8 +1031,7 @@ func TestGetters(t *testing.T) {
	if ctx == nil {
		t.Fatalf("Node context should exist")
	}
	nodeCtx, ok = ctx.(*NodeContext)
	if !ok || !validateNodeContext(nodeCtx, "demo1", "operator1", "zone1", "zone1-poa1", "") {
	if !validateNodeContext(ctx, "demo1", "operator1", "zone1", "zone1-poa1", "") {
		t.Fatalf("Invalid Operator context")
	}
	fmt.Println("Get Phy Location context")
@@ -1043,8 +1039,7 @@ func TestGetters(t *testing.T) {
	if ctx == nil {
		t.Fatalf("Node context should exist")
	}
	nodeCtx, ok = ctx.(*NodeContext)
	if !ok || !validateNodeContext(nodeCtx, "demo1", "operator1", "zone1", "zone1-poa1", "zone1-fog1") {
	if !validateNodeContext(ctx, "demo1", "operator1", "zone1", "zone1-poa1", "zone1-fog1") {
		t.Fatalf("Invalid Operator context")
	}
	fmt.Println("Get App context")
@@ -1052,8 +1047,7 @@ func TestGetters(t *testing.T) {
	if ctx == nil {
		t.Fatalf("Node context should exist")
	}
	nodeCtx, ok = ctx.(*NodeContext)
	if !ok || !validateNodeContext(nodeCtx, "demo1", "operator1", "zone1", "zone1-poa1", "ue1") {
	if !validateNodeContext(ctx, "demo1", "operator1", "zone1", "zone1-poa1", "ue1") {
		t.Fatalf("Invalid Operator context")
	}

Loading