Skip to content

Commit

Permalink
Ping default gw from withing the cluster to verify connectivity
Browse files Browse the repository at this point in the history
In case it fails it will rollback nmstate changes.

Signed-off-by: Quique Llorente <[email protected]>
  • Loading branch information
qinqon committed Oct 8, 2019
1 parent 06dded4 commit e5ee2f7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ push-handler: handler
docker push $(HANDLER_IMAGE)

test/unit: $(GINKGO)
INTERFACES_FILTER="" NODE_NAME=node01 $(GINKGO) $(UNIT_TEST_ARGS) ./pkg/
CONNECTIVITY_CHECK_TARGET="8.8.8.8" INTERFACES_FILTER="" NODE_NAME=node01 $(GINKGO) $(UNIT_TEST_ARGS) ./pkg/

test/e2e: $(OPERATOR_SDK)
$(OPERATOR_SDK) test local ./test/e2e \
Expand Down
2 changes: 1 addition & 1 deletion build/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM fedora:30

RUN sudo dnf install -y nmstate iproute && \
RUN sudo dnf install -y nmstate iproute iputils && \
sudo dnf clean all

# TODO: Delete this line after we update nmstate to include the change
Expand Down
6 changes: 6 additions & 0 deletions deploy/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ spec:
configMapKeyRef:
name: nmstate-config
key: interfaces_filter
- name: CONNECTIVITY_CHECK_TARGET
valueFrom:
configMapKeyRef:
name: nmstate-config
key: connectivity_check_target
volumeMounts:
- name: dbus-socket
mountPath: /run/dbus/system_bus_socket
Expand All @@ -76,3 +81,4 @@ metadata:
data:
node_network_state_refresh_interval: "5"
interfaces_filter: "veth*"
connectivity_check_target: "8.8.8.8"
30 changes: 29 additions & 1 deletion pkg/helper/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
yaml "sigs.k8s.io/yaml"

"github.com/tidwall/gjson"

"github.com/gobwas/glob"
nmstatev1alpha1 "github.com/nmstate/kubernetes-nmstate/pkg/apis/nmstate/v1alpha1"
)
Expand All @@ -22,7 +24,8 @@ const nmstateCommand = "nmstatectl"
const vlanFilteringCommand = "vlan-filtering"

var (
interfacesFilterGlob glob.Glob
interfacesFilterGlob glob.Glob
connectivityCheckTarget string
)

func init() {
Expand All @@ -31,6 +34,11 @@ func init() {
panic("INTERFACES_FILTER is mandatory")
}
interfacesFilterGlob = glob.MustCompile(interfacesFilter)

connectivityCheckTarget, isSet = os.LookupEnv("CONNECTIVITY_CHECK_TARGET")
if !isSet {
panic("CONNECTIVITY_CHECK_TARGET is mandatory")
}
}

func show(arguments ...string) (string, error) {
Expand Down Expand Up @@ -159,12 +167,26 @@ func UpdateCurrentState(client client.Client, nodeNetworkState *nmstatev1alpha1.
return nil
}

func ping(target string) error {
return exec.Command("ping", "-c", "3", target).Run()
}

func ApplyDesiredState(nodeNetworkState *nmstatev1alpha1.NodeNetworkState) (string, error) {
desiredState := string(nodeNetworkState.Spec.DesiredState)
if len(desiredState) == 0 {
return "Ignoring empty desired state", nil
}

currentState, err := yaml.YAMLToJSON([]byte(nodeNetworkState.Status.CurrentState))
if err != nil {
return "", fmt.Errorf("Impossible to convert current state to JSON")
}
defaultGwBeforeSet := gjson.ParseBytes([]byte(currentState)).
Get("routes.running.#(destination==\"0.0.0.0/0\").next-hop-address").String()
if defaultGwBeforeSet == "" {
return "", fmt.Errorf("Impossible to retrieve default gw")
}

setOutput, err := set(string(nodeNetworkState.Spec.DesiredState))
if err != nil {
return setOutput, err
Expand All @@ -188,10 +210,16 @@ func ApplyDesiredState(nodeNetworkState *nmstatev1alpha1.NodeNetworkState) (stri
}
}

err = ping(defaultGwBeforeSet)
if err != nil {
return commandOutput, rollback(fmt.Errorf("error pinging external address after network reconfiguration"))
}

_, err = commit()
if err != nil {
return commandOutput, rollback(err)
}

commandOutput += fmt.Sprintf("setOutput: %s \n", setOutput)
return commandOutput, nil
}
Expand Down
1 change: 0 additions & 1 deletion test/e2e/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ func deleteConnectionAtNodes(name string) []error {
}

func interfaces(state nmstatev1alpha1.State) []interface{} {
By("unmarshal state yaml into unstructured golang")
var stateUnstructured map[string]interface{}
err := yaml.Unmarshal(state, &stateUnstructured)
Expect(err).ToNot(HaveOccurred(), "Should parse correctly yaml: %s", state)
Expand Down

0 comments on commit e5ee2f7

Please sign in to comment.