Skip to content
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

create multus kubeconfig for incase of non auto flag. #1302

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions cmd/thin_entrypoint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,19 @@ func getFileAndHash(filepath string) ([]byte, []byte, error) {
return content, hash.Sum(nil), nil
}

func (o *Options) createKubeConfig(prevCAHash, prevSATokenHash []byte) ([]byte, []byte, error) {
caFileByte, caHash, err := getFileAndHash(serviceAccountCAFile)
func (o *Options) createKubeConfig(caFilePath, saTokenFilePath string, prevCAHash, prevSATokenHash []byte) ([]byte, []byte, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these mostly unused params necessary? looks like they're for testing, is there another way to handle it for the tests? thanks

// Use default paths if nil is passed
if caFilePath == "" {
caFilePath = serviceAccountCAFile
}
if saTokenFilePath == "" {
saTokenFilePath = serviceAccountTokenFile
}
caFileByte, caHash, err := getFileAndHash(caFilePath)
if err != nil {
return nil, nil, err
}
saTokenByte, saTokenHash, err := getFileAndHash(serviceAccountTokenFile)
saTokenByte, saTokenHash, err := getFileAndHash(saTokenFilePath)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -582,6 +589,11 @@ func main() {
var masterConfigFilePath string
// copy user specified multus conf to CNI conf directory
if opt.MultusConfFile != "auto" {
caHash, saTokenHash, err = opt.createKubeConfig("", "", nil, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create multus kubeconfig: %v\n", err)
return
}
confFileName := filepath.Base(opt.MultusConfFile)
tempConfFileName := fmt.Sprintf("%s.temp", confFileName)
if err = cmdutils.CopyFileAtomic(opt.MultusConfFile, opt.CNIConfDir, tempConfFileName, confFileName); err != nil {
Expand All @@ -590,7 +602,7 @@ func main() {
}
fmt.Printf("multus config file %s is copied.\n", opt.MultusConfFile)
} else { // auto generate multus config
caHash, saTokenHash, err = opt.createKubeConfig(nil, nil)
caHash, saTokenHash, err = opt.createKubeConfig("", "", nil, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create multus kubeconfig: %v\n", err)
return
Expand All @@ -608,7 +620,7 @@ func main() {
fmt.Printf("Entering watch loop...\n")
for {
// Check kubeconfig and update if different (i.e. service account updated)
caHash, saTokenHash, err = opt.createKubeConfig(caHash, saTokenHash)
caHash, saTokenHash, err = opt.createKubeConfig("", "", caHash, saTokenHash)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to update multus kubeconfig: %v\n", err)
return
Expand Down
39 changes: 39 additions & 0 deletions cmd/thin_entrypoint/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,4 +472,43 @@ var _ = Describe("thin entrypoint testing", func() {

Expect(os.RemoveAll(tmpDir)).To(Succeed())
})

It("Run createKubeConfig() with expected environment", func() {
// create directory and files
tmpDir, err := os.MkdirTemp("", "kubeconfig_test_tmp")
Expect(err).NotTo(HaveOccurred())

cniConfDir := fmt.Sprintf("%s/cni_conf", tmpDir)
Expect(os.Mkdir(cniConfDir, 0755)).To(Succeed())

// Set up the Options struct
options := &Options{
CNIConfDir: cniConfDir,
MultusCNIConfDir: "/tmp/multus/net.d",
}

// Create service account CA file and token file with dummy data
serviceAccountCAFile := fmt.Sprintf("%s/serviceAccountCAFile", tmpDir)
serviceAccountTokenFile := fmt.Sprintf("%s/serviceAccountTokenFile", tmpDir)
Expect(os.WriteFile(serviceAccountCAFile, []byte("dummy-ca-content"), 0644)).To(Succeed())
Expect(os.WriteFile(serviceAccountTokenFile, []byte("dummy-token-content"), 0644)).To(Succeed())

// Run the createKubeConfig function
caHash, saTokenHash, err := options.createKubeConfig(serviceAccountCAFile, serviceAccountTokenFile, nil, nil)
Expect(err).NotTo(HaveOccurred())

// Verify the hashes are not nil
Expect(caHash).NotTo(BeNil())
Expect(saTokenHash).NotTo(BeNil())

// Verify the kubeconfig file was created successfully
kubeConfigPath := fmt.Sprintf("%s/multus.d/multus.kubeconfig", options.CNIConfDir)
content, err := os.ReadFile(kubeConfigPath)
Expect(err).NotTo(HaveOccurred())
Expect(content).NotTo(BeEmpty())

// Cleanup
Expect(os.RemoveAll(tmpDir)).To(Succeed())
})

})