Skip to content

Commit

Permalink
Merge pull request #4 from kaloom/failure-handling
Browse files Browse the repository at this point in the history
Fix failure handing when creating auxiliary interfaces and fix create-kubeconfig.sh script
  • Loading branch information
kmabda authored Oct 26, 2020
2 parents f18fb1e + 62e6e19 commit d1a9e03
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 38 deletions.
63 changes: 27 additions & 36 deletions kactus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,30 +223,31 @@ func checkDelegate(netconf map[string]interface{}, masterpluginEnabled *bool) er
return nil
}

func delegateAdd(network kc.NetworkConfig, argif string, netconf map[string]interface{}, auxNetOnly bool) (bool, error) {
func delegateAdd(network kc.NetworkConfig, argif string, netconf map[string]interface{}, auxNetOnly bool) (error, types.Result) {
kc.LogDebug("delegateAdd: network '%v', argif '%s', netconf '%+v'\n", network, argif, netconf)
netconfBytes, err := json.Marshal(netconf)
if err != nil {
return true, fmt.Errorf("Kactus: error serializing kactus delegate netconf: %v", err)
return fmt.Errorf("Kactus: error serializing kactus delegate netconf: %v", err), nil
}

if !isMasterplugin(netconf) {
podif := kc.GetNetworkIfname(network.NetworkName)

if os.Setenv("CNI_IFNAME", podif) != nil {
return true, fmt.Errorf("Kactus: error in setting CNI_IFNAME")
return fmt.Errorf("Kactus: error in setting CNI_IFNAME"), nil
}
if network.IfMAC != "" {
cniArgs := fmt.Sprintf("IgnoreUnknown=1;CNI_IFMAC=%s", network.IfMAC)
if os.Setenv("CNI_ARGS", cniArgs); err != nil {
return true, fmt.Errorf("Kactus: error in setting CNI_ARGS to %s", cniArgs)
return fmt.Errorf("Kactus: error in setting CNI_ARGS to %s", cniArgs), nil
}
kc.LogDebug("delegateAdd: will invoke.DelegateAdd with a CNI_IFNAME set to: %s and CNI_ARGS set to: '%s' (not a master plugin)\n", podif, cniArgs)
} else {
kc.LogDebug("delegateAdd: will invoke.DelegateAdd with a CNI_IFNAME set to: %s (not a master plugin)\n", podif)
}
} else {
if os.Setenv("CNI_IFNAME", argif) != nil {
return true, fmt.Errorf("Kactus: error in setting CNI_IFNAME")
return fmt.Errorf("Kactus: error in setting CNI_IFNAME"), nil
}
kc.LogDebug("delegateAdd: will invoke.DelegateAdd with a CNI_IFNAME set to: %s (for master plugin)\n", argif)
}
Expand All @@ -256,17 +257,10 @@ func delegateAdd(network kc.NetworkConfig, argif string, netconf map[string]inte
result, err := invoke.DelegateAdd(delegatePluginType, netconfBytes)
if err != nil {
kc.LogError("delegateAdd: invoke.DelegateAdd errored: %s: %v\n", delegatePluginType, err)
return true, fmt.Errorf("Kactus: error in invoke Delegate add - %q: %v", delegatePluginType, err)
}

if !isMasterplugin(netconf) {
if auxNetOnly {
return true, result.Print()
}
return true, nil
return fmt.Errorf("Kactus: error in invoke Delegate add - %q: %v", delegatePluginType, err), nil
}

return false, result.Print()
return nil, result
}

func delegateDel(argIfName string, netconf map[string]interface{}) error {
Expand All @@ -291,14 +285,14 @@ func delegateDel(argIfName string, netconf map[string]interface{}) error {
return err
}

func clearPlugins(lastOkIdx int, idx int, argIfName string, delegates []map[string]interface{}) {
func clearPlugins(idx int, argIfName string, delegates []map[string]interface{}) {
if os.Setenv("CNI_COMMAND", "DEL") != nil {
kc.LogError("failed to set CNI_COMMAND to DEL")
return
}

kc.LogDebug("clearPlugins: lastOkIdx=%d, idx=%d, argIfName=%s\n", lastOkIdx, idx, argIfName)
for i := lastOkIdx + 1; i <= idx; i++ {
kc.LogDebug("clearPlugins: idx=%d, argIfName=%s\n", idx, argIfName)
for i := 0; i <= idx; i++ {
delegateDel(argIfName, delegates[i])
}
}
Expand Down Expand Up @@ -568,38 +562,35 @@ func cmdAdd(args *skel.CmdArgs) error {
}
}

currentDelegates, err := saveDelegates(args.ContainerID, nc.CNIDir, true, nc.Delegates)
if err != nil {
err = fmt.Errorf("Kactus: Err in saving the delegates: %v", err)
kc.LogError("cmdAdd: %v\n", err)
return err
}

var lastErr error
lastOkIdx, idx := -1, -1
var result types.Result
idx := -1
for i, delegate := range nc.Delegates {
idx = i
if nc.CNIVersion != "" {
delegate["cniVersion"] = nc.CNIVersion
}
errored, err := delegateAdd(networks[i], args.IfName, delegate, auxNetOnly)
if !errored {
lastOkIdx = i
} else if errored && err != nil {
lastErr = err
err, result = delegateAdd(networks[i], args.IfName, delegate, auxNetOnly)
if err != nil {
kc.LogError("cmdAdd: %v\n", err)
break
}
}

if lastErr != nil {
clearPlugins(lastOkIdx, idx, args.IfName, nc.Delegates)
saveDelegates(args.ContainerID, nc.CNIDir, false, currentDelegates)
return lastErr
if err != nil {
clearPlugins(idx, args.IfName, nc.Delegates)
return err
}

_, err = saveDelegates(args.ContainerID, nc.CNIDir, true, nc.Delegates)
if err != nil {
err = fmt.Errorf("Kactus: Err in saving the delegates: %v", err)
kc.LogError("cmdAdd: %v\n", err)
return err
}

kc.LogInfo("cmdAdd: delegated the creation of networks %+v\n", networks)

return nil
return result.Print()
}

func cmdDel(args *skel.CmdArgs) error {
Expand Down
6 changes: 4 additions & 2 deletions scripts/create-kubeconfig.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/bin/sh
#!/bin/bash

set -eo pipefail

cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"

config_dir=${1:-/tmp/kubeconfig}
kubecfg_file=${2:-kactus-kubeconfig.yaml}
namespace=${3:-kube-system}
Expand All @@ -13,7 +15,7 @@ fi
kubecfg_path=${config_dir}/${kubecfg_file}

# create the service account and RBAC permissions
kubectl apply -f kactus-serviceaccount-and-rbac.yaml
kubectl apply -f ../manifests/kactus-serviceaccount-and-rbac.yaml
# get the secret name from the service account
secret_name=$(kubectl get sa kactus -n $namespace -o jsonpath="{.secrets[*].name}")
# extract the ca.crt from the secret
Expand Down

0 comments on commit d1a9e03

Please sign in to comment.