Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
related calico param should be set when cni is calico
Browse files Browse the repository at this point in the history
  • Loading branch information
xiujuanx committed Dec 12, 2022
1 parent cee415d commit 08c8255
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
25 changes: 22 additions & 3 deletions tkg/client/client_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1394,14 +1394,14 @@ var _ = Describe("ValidateCNIConfig", func() {
JustBeforeEach(func() {
tkgClient, err = CreateTKGClient(tkgConfigPath, testingDir, defaultTKGBoMFileForTesting, 2*time.Second)
Expect(err).NotTo(HaveOccurred())
err = tkgClient.ConfigureAndValidateCNIType(cniType)
})

Context("when --cni option is specified with antrea", func() {
BeforeEach(func() {
cniType = "antrea"
})
It("should use antrea as cni provider", func() {
err = tkgClient.ConfigureAndValidateCNIType(cniType)
Expect(err).To(Not(HaveOccurred()))
Expect(tkgClient.TKGConfigReaderWriter().Get(constants.ConfigVariableCNI)).To(Equal("antrea"))
})
Expand All @@ -1411,9 +1411,16 @@ var _ = Describe("ValidateCNIConfig", func() {
BeforeEach(func() {
cniType = "calico"
})
It("should return an error when _ALLOW_CALICO_ON_MANAGEMENT_CLUSTER is not true", func() {
err = tkgClient.ConfigureAndValidateCNIType(cniType)
Expect(err.Error()).To(Equal(fmt.Sprintf("_ALLOW_CALICO_ON_MANAGEMENT_CLUSTER env should be true when use calico cni, however it's '%s'", os.Getenv("_ALLOW_CALICO_ON_MANAGEMENT_CLUSTER"))))
})

It("should use calico as cni provider", func() {
Expect(err).To(Not(HaveOccurred()))
os.Setenv("_ALLOW_CALICO_ON_MANAGEMENT_CLUSTER", "true")
err = tkgClient.ConfigureAndValidateCNIType(cniType)
Expect(tkgClient.TKGConfigReaderWriter().Get(constants.ConfigVariableCNI)).To(Equal("calico"))
os.Setenv("_ALLOW_CALICO_ON_MANAGEMENT_CLUSTER", "false")
})
})

Expand All @@ -1422,6 +1429,7 @@ var _ = Describe("ValidateCNIConfig", func() {
cniType = "none"
})
It("should use none as cni provider", func() {
err = tkgClient.ConfigureAndValidateCNIType(cniType)
Expect(err).To(Not(HaveOccurred()))
Expect(tkgClient.TKGConfigReaderWriter().Get(constants.ConfigVariableCNI)).To(Equal("none"))
})
Expand All @@ -1432,6 +1440,7 @@ var _ = Describe("ValidateCNIConfig", func() {
cniType = "fake-cni"
})
It("should use throw an error", func() {
err = tkgClient.ConfigureAndValidateCNIType(cniType)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("provided CNI type 'fake-cni' is not in the available options: antrea, calico, none"))
})
Expand All @@ -1441,7 +1450,8 @@ var _ = Describe("ValidateCNIConfig", func() {
BeforeEach(func() {
cniType = ""
})
It("should use calico as cni provider", func() {
It("should use antrea as cni provider", func() {
err = tkgClient.ConfigureAndValidateCNIType(cniType)
Expect(err).To(Not(HaveOccurred()))
Expect(tkgClient.TKGConfigReaderWriter().Get(constants.ConfigVariableCNI)).To(Equal("antrea"))
})
Expand All @@ -1453,9 +1463,18 @@ var _ = Describe("ValidateCNIConfig", func() {
err = os.WriteFile(tkgConfigPath, []byte("CNI: calico"), constants.ConfigFilePermissions)
Expect(err).ToNot(HaveOccurred())
})

It("should use calico as cni provider", func() {
os.Setenv("_ALLOW_CALICO_ON_MANAGEMENT_CLUSTER", "true")
err = tkgClient.ConfigureAndValidateCNIType(cniType)
Expect(err).To(Not(HaveOccurred()))
Expect(tkgClient.TKGConfigReaderWriter().Get(constants.ConfigVariableCNI)).To(Equal("calico"))
os.Setenv("_ALLOW_CALICO_ON_MANAGEMENT_CLUSTER", "false")
})

It("should return an error when _ALLOW_CALICO_ON_MANAGEMENT_CLUSTER is not true", func() {
err = tkgClient.ConfigureAndValidateCNIType(cniType)
Expect(err.Error()).To(Equal(fmt.Sprintf("_ALLOW_CALICO_ON_MANAGEMENT_CLUSTER env should be true when use calico cni, however it's '%s'", os.Getenv("_ALLOW_CALICO_ON_MANAGEMENT_CLUSTER"))))
})
})

Expand Down
8 changes: 8 additions & 0 deletions tkg/client/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,14 @@ func (c *TkgClient) ConfigureAndValidateCNIType(cniType string) error {
if _, ok := CNITypes[cniType]; !ok {
return errors.Errorf("provided CNI type '%s' is not in the available options: antrea, calico, none", cniType)
}

// _ALLOW_CALICO_ON_MANAGEMENT_CLUSTER must be true when cniType is calico
if cniType == "calico" {
allowCalicoType := os.Getenv("_ALLOW_CALICO_ON_MANAGEMENT_CLUSTER")
if allowCalicoType != "true" {
return errors.Errorf("_ALLOW_CALICO_ON_MANAGEMENT_CLUSTER env should be true when use calico cni, however it's '%s'", allowCalicoType)
}
}
c.TKGConfigReaderWriter().Set(constants.ConfigVariableCNI, cniType)
return nil
}
Expand Down

0 comments on commit 08c8255

Please # to comment.