-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use github.com/gofrs/flock to lock handler #731
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package file | ||
|
||
import ( | ||
"os" | ||
"time" | ||
) | ||
|
||
func Touch(fileName string) error { | ||
_, err := os.Stat(fileName) | ||
if os.IsNotExist(err) { | ||
file, err := os.Create(fileName) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
} else { | ||
currentTime := time.Now().Local() | ||
err = os.Chtimes(fileName, currentTime, currentTime) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,46 +7,35 @@ import ( | |
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
nmstatev1beta1 "github.com/nmstate/kubernetes-nmstate/api/v1beta1" | ||
"github.com/nmstate/kubernetes-nmstate/test/e2e/daemonset" | ||
"github.com/nmstate/kubernetes-nmstate/test/e2e/deployment" | ||
|
||
"github.com/nmstate/kubernetes-nmstate/test/cmd" | ||
testenv "github.com/nmstate/kubernetes-nmstate/test/env" | ||
) | ||
|
||
var ( | ||
defaultNMState = nmstatev1beta1.NMState{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "nmstate", | ||
Namespace: "nmstate", | ||
}, | ||
} | ||
webhookKey = types.NamespacedName{Namespace: "nmstate", Name: "nmstate-webhook"} | ||
handlerKey = types.NamespacedName{Namespace: "nmstate", Name: "nmstate-handler"} | ||
handlerLabels = map[string]string{"component": "kubernetes-nmstate-handler"} | ||
) | ||
|
||
var _ = Describe("NMState operator", func() { | ||
Context("when installed for the first time", func() { | ||
BeforeEach(func() { | ||
installDefaultNMState() | ||
installNMState(defaultNMState) | ||
}) | ||
It("should deploy daemonset and webhook deployment", func() { | ||
daemonset.GetEventually(handlerKey).Should(daemonset.BeReady()) | ||
deployment.GetEventually(webhookKey).Should(deployment.BeReady()) | ||
}) | ||
AfterEach(func() { | ||
uninstallDefaultNMState() | ||
uninstallNMState(defaultNMState) | ||
}) | ||
}) | ||
Context("when NMState is installed", func() { | ||
It("should list one NMState CR", func() { | ||
installDefaultNMState() | ||
installNMState(defaultNMState) | ||
daemonset.GetEventually(handlerKey).Should(daemonset.BeReady()) | ||
ds, err := daemonset.GetList(handlerLabels) | ||
Expect(err).ToNot(HaveOccurred(), "List daemon sets in namespace nmstate succeeds") | ||
|
@@ -71,7 +60,7 @@ var _ = Describe("NMState operator", func() { | |
}) | ||
Context("and uninstalled", func() { | ||
BeforeEach(func() { | ||
uninstallDefaultNMState() | ||
uninstallNMState(defaultNMState) | ||
}) | ||
It("should uninstall handler and webhook", func() { | ||
Eventually(func() bool { | ||
|
@@ -85,24 +74,55 @@ var _ = Describe("NMState operator", func() { | |
}) | ||
}) | ||
}) | ||
Context("when another handler is installed with different namespace", func() { | ||
var ( | ||
operatorNamespace = "nmstate-alt" | ||
) | ||
BeforeEach(func() { | ||
installNMState(defaultNMState) | ||
daemonset.GetEventually(handlerKey).Should(daemonset.BeReady()) | ||
installOperator(operatorNamespace) | ||
}) | ||
AfterEach(func() { | ||
uninstallNMState(defaultNMState) | ||
uninstallOperator(operatorNamespace) | ||
installOperator("nmstate") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is the operator installed in "nmstate" namespace at the end? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test-e2e-operator test suite spec the operator to be installed, but the test we are adding here remove the nmstate operator to check that the handler installed from different namespace is blocked until the "nmstate" one is delete. The install here ensure that after that test the nmstate operator is there as expected at the other test cases. |
||
}) | ||
It("should wait on the old one to be deleted", func() { | ||
By("Checking handler is locked") | ||
daemonset.GetConsistently(types.NamespacedName{Namespace: operatorNamespace, Name: "nmstate-handler"}).ShouldNot(daemonset.BeReady()) | ||
uninstallOperator("nmstate") | ||
By("Checking handler is unlocked after deleting old one") | ||
daemonset.GetEventually(types.NamespacedName{Namespace: operatorNamespace, Name: "nmstate-handler"}).Should(daemonset.BeReady()) | ||
}) | ||
}) | ||
}) | ||
|
||
func installNMState(nmstate nmstatev1beta1.NMState) { | ||
err := testenv.Client.Create(context.TODO(), &nmstate) | ||
Expect(err).ToNot(HaveOccurred(), "NMState CR created without error") | ||
} | ||
|
||
func installDefaultNMState() { | ||
installNMState(defaultNMState) | ||
} | ||
func installOperator(namespace string) error { | ||
By(fmt.Sprintf("Creating NMState operator with namespace '%s'", namespace)) | ||
_, err := cmd.Run("make", false, fmt.Sprintf("OPERATOR_NAMESPACE=%s", namespace), fmt.Sprintf("HANDLER_NAMESPACE=%s", namespace), "IMAGE_REGISTRY=registry:5000", "manifests") | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
func uninstallNMState(nmstate nmstatev1beta1.NMState) { | ||
err := testenv.Client.Delete(context.TODO(), &nmstate, &client.DeleteOptions{}) | ||
if !apierrors.IsNotFound(err) { | ||
Expect(err).ToNot(HaveOccurred(), "NMState CR successfully removed") | ||
manifestsDir := "build/_output/manifests/" | ||
manifests := []string{"namespace.yaml", "service_account.yaml", "operator.yaml", "role.yaml", "role_binding.yaml"} | ||
for _, manifest := range manifests { | ||
_, err = cmd.Kubectl("apply", "-f", manifestsDir+manifest) | ||
Expect(err).ToNot(HaveOccurred()) | ||
} | ||
deployment.GetEventually(types.NamespacedName{Namespace: namespace, Name: "nmstate-operator"}).Should(deployment.BeReady()) | ||
|
||
return nil | ||
} | ||
|
||
func uninstallDefaultNMState() { | ||
uninstallNMState(defaultNMState) | ||
func uninstallOperator(namespace string) { | ||
By(fmt.Sprintf("Deleting namespace '%s'", namespace)) | ||
ns := corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: namespace, | ||
}, | ||
} | ||
Expect(testenv.Client.Delete(context.TODO(), &ns)).To(SatisfyAny(Succeed(), WithTransform(apierrors.IsNotFound, BeTrue()))) | ||
Eventually(func() error { | ||
return testenv.Client.Get(context.TODO(), types.NamespacedName{Name: namespace}, &ns) | ||
}, 2*time.Minute, 5*time.Second).Should(SatisfyAll(HaveOccurred(), WithTransform(apierrors.IsNotFound, BeTrue()))) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This message sounds odd, we don't mark handler healthy here, do we? That's what the probe will do once it tries to cat that file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well we mark the container by touching the file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, I understand now