-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
policy,init: do not start policy in exclusive eni mode
Signed-off-by: l1b0k <[email protected]>
- Loading branch information
Showing
14 changed files
with
606 additions
and
52 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
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,173 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
k8sClient "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/AliyunContainerService/terway/pkg/utils/nodecap" | ||
"github.com/AliyunContainerService/terway/pkg/version" | ||
"github.com/AliyunContainerService/terway/types" | ||
"github.com/AliyunContainerService/terway/types/daemon" | ||
) | ||
|
||
const eniOnlyCNI = `{ | ||
"cniVersion": "0.4.0", | ||
"name": "terway-chainer", | ||
"plugins": [ | ||
{ | ||
"capabilities": { | ||
"bandwidth": true | ||
}, | ||
"host_stack_cidrs": [ | ||
"169.254.20.10/32" | ||
], | ||
"type": "terway" | ||
} | ||
] | ||
}` | ||
|
||
const cniFilePath = "/etc/cni/net.d/10-terway.conflist" | ||
const nodeCapabilitiesFile = "/var/run/eni/node_capabilities" | ||
|
||
type Task struct { | ||
Name string | ||
Func func(cmd *cobra.Command, args []string) error | ||
} | ||
|
||
var tasks = []Task{ | ||
{ | ||
Name: "get eni config", | ||
Func: getENIConfig, | ||
}, | ||
{ | ||
Name: "eniOnly", | ||
Func: overrideCNI, | ||
}, | ||
{ | ||
Name: "dual stack", | ||
Func: dualStack, | ||
}, | ||
} | ||
|
||
var nodeconfigCmd = &cobra.Command{ | ||
Use: "nodeconfig", | ||
SilenceUsage: true, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
for _, task := range tasks { | ||
err := task.Func(cmd, args) | ||
if err != nil { | ||
_, _ = fmt.Fprintf(os.Stderr, "task: %serror: %v\n", task.Name, err) | ||
os.Exit(1) | ||
} | ||
} | ||
}, | ||
} | ||
|
||
var eniCfg *daemon.Config | ||
|
||
func getENIConfig(cmd *cobra.Command, args []string) error { | ||
var err error | ||
eniCfg, err = daemon.GetConfigFromFileWithMerge("/etc/eni/eni.json", nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("eni config: %+v\n", eniCfg) | ||
return nil | ||
} | ||
|
||
func overrideCNI(cmd *cobra.Command, args []string) error { | ||
restConfig := ctrl.GetConfigOrDie() | ||
restConfig.UserAgent = version.UA | ||
c, err := k8sClient.New(restConfig, k8sClient.Options{ | ||
Scheme: types.Scheme, | ||
Mapper: types.NewRESTMapper(), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
node := &corev1.Node{} | ||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) | ||
defer cancel() | ||
|
||
nodeName := os.Getenv("K8S_NODE_NAME") | ||
|
||
err = c.Get(ctx, k8sClient.ObjectKey{ | ||
Name: nodeName, | ||
}, node, &k8sClient.GetOptions{ | ||
Raw: &metav1.GetOptions{ | ||
ResourceVersion: "0", | ||
}, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("get node %s error: %w", nodeName, err) | ||
} | ||
|
||
store := nodecap.NewFileNodeCapabilities(nodeCapabilitiesFile) | ||
return setExclusiveMode(store, node.Labels, cniFilePath) | ||
} | ||
|
||
func setExclusiveMode(store nodecap.NodeCapabilitiesStore, labels map[string]string, cniPath string) error { | ||
err := store.Load() | ||
if err != nil { | ||
return err | ||
} | ||
now := types.NodeExclusiveENIMode(labels) | ||
|
||
switch store.Get(nodecap.NodeCapabilityExclusiveENI) { | ||
case string(types.ExclusiveENIOnly): | ||
if now != types.ExclusiveENIOnly { | ||
return fmt.Errorf("exclusive eni mode changed") | ||
} | ||
case string(types.ExclusiveDefault): | ||
if now != types.ExclusiveDefault { | ||
return fmt.Errorf("exclusive eni mode changed") | ||
} | ||
case "": | ||
// empty for new node, or rebooted | ||
} | ||
|
||
store.Set(nodecap.NodeCapabilityExclusiveENI, string(now)) | ||
err = store.Save() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// write cni config | ||
if now == types.ExclusiveENIOnly { | ||
err = os.WriteFile(cniPath, []byte(eniOnlyCNI), 0644) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func dualStack(cmd *cobra.Command, args []string) error { | ||
store := nodecap.NewFileNodeCapabilities(nodeCapabilitiesFile) | ||
|
||
val := "" | ||
switch eniCfg.IPStack { | ||
case "dual", "ipv6": | ||
val = "true" | ||
default: | ||
val = "false" | ||
} | ||
|
||
err := store.Load() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
store.Set(nodecap.NodeCapabilityIPv6, val) | ||
return store.Save() | ||
} |
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,94 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/AliyunContainerService/terway/pkg/utils/nodecap" | ||
) | ||
|
||
func TestExclusiveModeNewNode(t *testing.T) { | ||
tempFile, err := os.CreateTemp("", "test_node_capabilities") | ||
assert.NoError(t, err) | ||
defer os.Remove(tempFile.Name()) | ||
|
||
store := nodecap.NewFileNodeCapabilities(tempFile.Name()) | ||
labels := map[string]string{"k8s.aliyun.com/exclusive-mode-eni-type": "eniOnly"} | ||
cniPath := tempFile.Name() + "_cni_config" | ||
|
||
err = setExclusiveMode(store, labels, cniPath) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "eniOnly", store.Get(nodecap.NodeCapabilityExclusiveENI)) | ||
} | ||
|
||
func TestExclusiveModeDoesNotChangeWhenAlreadySet(t *testing.T) { | ||
tempFile, err := os.CreateTemp("", "test_node_capabilities") | ||
assert.NoError(t, err) | ||
defer os.Remove(tempFile.Name()) | ||
|
||
err = os.WriteFile(tempFile.Name(), []byte("cni_exclusive_eni = eniOnly"), 0644) | ||
assert.NoError(t, err) | ||
|
||
store := nodecap.NewFileNodeCapabilities(tempFile.Name()) | ||
labels := map[string]string{"k8s.aliyun.com/exclusive-mode-eni-type": "eniOnly"} | ||
cniPath := tempFile.Name() + "_cni_config" | ||
|
||
err = setExclusiveMode(store, labels, cniPath) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "eniOnly", store.Get(nodecap.NodeCapabilityExclusiveENI)) | ||
} | ||
|
||
func TestExclusiveModeFailsWhenChangedFromExclusive(t *testing.T) { | ||
tempFile, err := os.CreateTemp("", "test_node_capabilities") | ||
assert.NoError(t, err) | ||
defer os.Remove(tempFile.Name()) | ||
|
||
err = os.WriteFile(tempFile.Name(), []byte("cni_exclusive_eni = eniOnly"), 0644) | ||
assert.NoError(t, err) | ||
|
||
store := nodecap.NewFileNodeCapabilities(tempFile.Name()) | ||
labels := map[string]string{"k8s.aliyun.com/exclusive-mode-eni-type": "default"} | ||
cniPath := tempFile.Name() + "_cni_config" | ||
|
||
err = setExclusiveMode(store, labels, cniPath) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "exclusive eni mode changed") | ||
} | ||
|
||
func TestExclusiveModeWritesCNIConfigWhenSet(t *testing.T) { | ||
tempFile, err := os.CreateTemp("", "test_node_capabilities") | ||
assert.NoError(t, err) | ||
defer os.Remove(tempFile.Name()) | ||
|
||
store := nodecap.NewFileNodeCapabilities(tempFile.Name()) | ||
labels := map[string]string{"k8s.aliyun.com/exclusive-mode-eni-type": "eniOnly"} | ||
cniPath := tempFile.Name() + "_cni_config" | ||
defer os.Remove(cniPath) | ||
|
||
err = setExclusiveMode(store, labels, cniPath) | ||
assert.NoError(t, err) | ||
|
||
content, err := os.ReadFile(cniPath) | ||
assert.NoError(t, err) | ||
assert.Contains(t, string(content), `"type": "terway"`) | ||
} | ||
|
||
func TestExclusiveModeDoesNotWriteCNIConfigWhenNotSet(t *testing.T) { | ||
tempFile, err := os.CreateTemp("", "test_node_capabilities") | ||
assert.NoError(t, err) | ||
defer os.Remove(tempFile.Name()) | ||
|
||
store := nodecap.NewFileNodeCapabilities(tempFile.Name()) | ||
labels := map[string]string{"k8s.aliyun.com/exclusive-mode-eni-type": "default"} | ||
cniPath := tempFile.Name() + "_cni_config" | ||
defer os.Remove(cniPath) | ||
|
||
err = setExclusiveMode(store, labels, cniPath) | ||
assert.NoError(t, err) | ||
|
||
_, err = os.ReadFile(cniPath) | ||
assert.Error(t, err) | ||
assert.True(t, os.IsNotExist(err)) | ||
} |
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
Oops, something went wrong.