func generateJSON()

in cmd/aws-vpc-cni/main.go [213:333]


func generateJSON(jsonFile string, outFile string, getPrimaryIP func(ipv4 bool) (string, error)) error {
	byteValue, err := os.ReadFile(jsonFile)
	if err != nil {
		return err
	}

	// enabledIPv6 is to determine if EKS cluster is IPv4 or IPv6 cluster
	// if this EKS cluster is IPv6 cluster, egress-cni-plugin will enable IPv4 egress by default
	// if this EKS cluster is IPv4 cluster, egress-cni-plugin will only enable IPv6 egress if env var "ENABLE_V6_EGRESS" is "true"
	enabledIPv6 := utils.GetBoolAsStringEnvVar(envEnIPv6, defaultEnableIPv6)
	var egressIPAMSubnet string
	var egressIPAMDst string
	var egressIPAMDataDir string
	var egressEnabled bool
	var egressPluginLogFile string
	var nodeIP = ""
	if enabledIPv6 {
		// EKS IPv6 cluster
		egressIPAMSubnet = egressPluginIpamSubnetV4
		egressIPAMDst = egressPluginIpamDstV4
		egressIPAMDataDir = egressPluginIpamDataDirV4
		// Enable IPv4 egress when "ENABLE_V4_EGRESS" is "true" (default)
		egressEnabled = utils.GetBoolAsStringEnvVar(envEnIPv4Egress, defaultEnableIPv4Egress)
		egressPluginLogFile = utils.GetEnv(envEgressV4PluginLogFile, defaultEgressV4PluginLogFile)
		nodeIP, err = getPrimaryIP(true)
		// Node should have a IPv4 address even in IPv6 cluster
		if err != nil {
			log.Errorf("Failed to get Node IP, error: %v", err)
			return err
		}
	} else {
		// EKS IPv4 cluster
		egressIPAMSubnet = egressPluginIpamSubnetV6
		egressIPAMDst = egressPluginIpamDstV6
		egressIPAMDataDir = egressPluginIpamDataDirV6
		egressPluginLogFile = utils.GetEnv(envEgressV6PluginLogFile, defaultEgressV6PluginLogFile)
		egressEnabled = utils.GetBoolAsStringEnvVar(envEnIPv6Egress, defaultEnableIPv6Egress)
		if egressEnabled {
			nodeIP, err = getPrimaryIP(false)
			if err != nil {
				// When ENABLE_V6_EGRESS is set, but the node is lacking an IPv6 address, log a warning and disable the egress-v6-cni plugin.
				// This allows IPv4-only nodes to function while still alerting the customer to the possibility of a misconfiguration.
				log.Warnf("To support IPv6 egress, node primary ENI must have a global IPv6 address, error: %v", err)
				egressEnabled = false
			}
		}
	}
	vethPrefix := utils.GetEnv(envVethPrefix, defaultVethPrefix)
	// Derive pod MTU from ENI MTU by default (note that values have already been validated)
	eniMTU := utils.GetEnv(envEniMTU, strconv.Itoa(defaultMTU))
	// If pod MTU environment variable is set, overwrite ENI MTU.
	podMTU := utils.GetEnv(envPodMTU, eniMTU)
	podSGEnforcingMode := utils.GetEnv(envPodSGEnforcingMode, defaultPodSGEnforcingMode)
	pluginLogFile := utils.GetEnv(envPluginLogFile, defaultPluginLogFile)
	pluginLogLevel := utils.GetEnv(envPluginLogLevel, defaultPluginLogLevel)
	randomizeSNAT := utils.GetEnv(envRandomizeSNAT, defaultRandomizeSNAT)

	netconf := string(byteValue)
	netconf = strings.Replace(netconf, "__VETHPREFIX__", vethPrefix, -1)
	netconf = strings.Replace(netconf, "__MTU__", podMTU, -1)
	netconf = strings.Replace(netconf, "__PODSGENFORCINGMODE__", podSGEnforcingMode, -1)
	netconf = strings.Replace(netconf, "__PLUGINLOGFILE__", pluginLogFile, -1)
	netconf = strings.Replace(netconf, "__PLUGINLOGLEVEL__", pluginLogLevel, -1)
	netconf = strings.Replace(netconf, "__EGRESSPLUGINLOGFILE__", egressPluginLogFile, -1)
	netconf = strings.Replace(netconf, "__EGRESSPLUGINENABLED__", strconv.FormatBool(egressEnabled), -1)
	netconf = strings.Replace(netconf, "__EGRESSPLUGINIPAMSUBNET__", egressIPAMSubnet, -1)
	netconf = strings.Replace(netconf, "__EGRESSPLUGINIPAMDST__", egressIPAMDst, -1)
	netconf = strings.Replace(netconf, "__EGRESSPLUGINIPAMDATADIR__", egressIPAMDataDir, -1)
	netconf = strings.Replace(netconf, "__RANDOMIZESNAT__", randomizeSNAT, -1)
	netconf = strings.Replace(netconf, "__NODEIP__", nodeIP, -1)

	byteValue = []byte(netconf)

	// Chain any requested CNI plugins
	enBandwidthPlugin := utils.GetBoolAsStringEnvVar(envEnBandwidthPlugin, defaultEnBandwidthPlugin)
	disablePodV6 := utils.GetBoolAsStringEnvVar(envDisablePodV6, defaultDisablePodV6)
	if enBandwidthPlugin || disablePodV6 {
		// Unmarshall current conflist into data
		data := NetConfList{}
		err = json.Unmarshal(byteValue, &data)
		if err != nil {
			return err
		}

		// Chain the bandwidth plugin when enabled
		if enBandwidthPlugin {
			bwPlugin := NetConf{
				Type:         "bandwidth",
				Capabilities: map[string]bool{"bandwidth": true},
			}
			data.Plugins = append(data.Plugins, &bwPlugin)
		}

		// Chain the tuning plugin (configured to disable IPv6 in pod network namespace) when requested
		if disablePodV6 {
			tuningPlugin := NetConf{
				Type: "tuning",
				Sysctl: map[string]string{
					"net.ipv6.conf.all.disable_ipv6":     "1",
					"net.ipv6.conf.default.disable_ipv6": "1",
					"net.ipv6.conf.lo.disable_ipv6":      "1",
				},
			}
			data.Plugins = append(data.Plugins, &tuningPlugin)
		}

		// Marshall data back into byteValue
		byteValue, err = json.MarshalIndent(data, "", "  ")
		if err != nil {
			return err
		}
	}

	err = isValidJSON(string(byteValue))
	if err != nil {
		log.Fatalf("%s is not a valid json object, error: %s", netconf, err)
	}

	err = os.WriteFile(outFile, byteValue, 0644)
	return err
}