-
Notifications
You must be signed in to change notification settings - Fork 584
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 in case of non auto flag. #1317
Merged
dougbtv
merged 1 commit into
k8snetworkplumbingwg:master
from
adrianchiris:pr-1302-update
Aug 1, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,35 @@ package main | |
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"syscall" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" //nolint:golint | ||
. "github.com/onsi/gomega" //nolint:golint | ||
) | ||
|
||
// chrootTestHelper performs chroot syscall, returns func to get back to original root or error if occurred | ||
func chrootTestHelper(path string) (func() error, error) { | ||
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. smart! |
||
root, err := os.Open("/") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := syscall.Chroot(path); err != nil { | ||
root.Close() | ||
return nil, err | ||
} | ||
|
||
return func() error { | ||
defer root.Close() | ||
if err := root.Chdir(); err != nil { | ||
return err | ||
} | ||
return syscall.Chroot(".") | ||
}, nil | ||
} | ||
|
||
func TestThinEntrypoint(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "thin_entrypoint") | ||
|
@@ -472,4 +495,50 @@ var _ = Describe("thin entrypoint testing", func() { | |
|
||
Expect(os.RemoveAll(tmpDir)).To(Succeed()) | ||
}) | ||
|
||
It("Run createKubeConfig()", func() { | ||
// create temp dir and files | ||
tmpDir := GinkgoT().TempDir() | ||
|
||
cniConfDir := "/cni_conf" | ||
Expect(os.Mkdir(filepath.Join(tmpDir, cniConfDir), 0755)).To(Succeed()) | ||
|
||
multusConfDir := "/multus_conf" | ||
Expect(os.Mkdir(filepath.Join(tmpDir, multusConfDir), 0755)).To(Succeed()) | ||
|
||
// Create service account CA file and token file with dummy data | ||
svcAccountPath := filepath.Join(tmpDir, "var/run/secrets/kubernetes.io/serviceaccount") | ||
Expect(os.MkdirAll(svcAccountPath, 0755)).ToNot(HaveOccurred()) | ||
svcAccountCAFile := filepath.Join(tmpDir, serviceAccountCAFile) | ||
svcAccountTokenFile := filepath.Join(tmpDir, serviceAccountTokenFile) | ||
Expect(os.WriteFile(svcAccountCAFile, []byte("dummy-ca-content"), 0644)).To(Succeed()) | ||
Expect(os.WriteFile(svcAccountTokenFile, []byte("dummy-token-content"), 0644)).To(Succeed()) | ||
|
||
// Set up the Options struct | ||
options := &Options{ | ||
CNIConfDir: cniConfDir, | ||
MultusCNIConfDir: multusConfDir, | ||
} | ||
|
||
// Run the createKubeConfig function in a chroot env | ||
back, err := chrootTestHelper(tmpDir) | ||
Expect(err).ToNot(HaveOccurred()) | ||
caHash, saTokenHash, err := options.createKubeConfig(nil, nil) | ||
Expect(back()).ToNot(HaveOccurred()) | ||
// back to original root | ||
|
||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(caHash).NotTo(BeNil()) | ||
Expect(saTokenHash).NotTo(BeNil()) | ||
|
||
// Verify the kubeconfig file was created successfully | ||
kubeConfigPath := filepath.Join(tmpDir, cniConfDir, "multus.d", "multus.kubeconfig") | ||
content, err := os.ReadFile(kubeConfigPath) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(content).NotTo(BeEmpty()) | ||
|
||
// Cleanup | ||
Expect(os.RemoveAll(tmpDir)).To(Succeed()) | ||
}) | ||
|
||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nice and efficient.