From 50255a76015c85f548549e515305e8dda9075b9c Mon Sep 17 00:00:00 2001 From: Magnus Kulke Date: Fri, 9 Aug 2024 15:17:19 +0200 Subject: [PATCH] initdata: don't fail process-user-data when absent currently the process-user-data unit will fail because process-user-data will exit with a non-zero exit code if there is no initdata file on the file system. this PR will skip the initdata processing with a log message and exit successfully in such a case. Signed-off-by: Magnus Kulke --- src/cloud-api-adaptor/pkg/userdata/provision.go | 11 +++++++++-- .../pkg/userdata/provision_test.go | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/cloud-api-adaptor/pkg/userdata/provision.go b/src/cloud-api-adaptor/pkg/userdata/provision.go index aeda2bdca..cc5f2909e 100644 --- a/src/cloud-api-adaptor/pkg/userdata/provision.go +++ b/src/cloud-api-adaptor/pkg/userdata/provision.go @@ -6,6 +6,7 @@ import ( "crypto/sha512" "encoding/base64" "encoding/hex" + "errors" "fmt" "log" "os" @@ -207,11 +208,17 @@ func processCloudConfig(cfg *Config, cc *CloudConfig) error { } func extractInitdataAndHash(cfg *Config) error { - if _, err := os.Stat(cfg.initdataPath); err != nil { + path := cfg.initdataPath + _, err := os.Stat(path) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + logger.Printf("File %s not found, skipped initdata processing.\n", path) + return nil + } return fmt.Errorf("Error stat initdata file: %w", err) } - dataBytes, err := os.ReadFile(cfg.initdataPath) + dataBytes, err := os.ReadFile(path) if err != nil { return fmt.Errorf("Error read initdata file: %w", err) } diff --git a/src/cloud-api-adaptor/pkg/userdata/provision_test.go b/src/cloud-api-adaptor/pkg/userdata/provision_test.go index f1a95ac54..bbce41330 100644 --- a/src/cloud-api-adaptor/pkg/userdata/provision_test.go +++ b/src/cloud-api-adaptor/pkg/userdata/provision_test.go @@ -614,6 +614,22 @@ func TestExtractInitdataAndHash(t *testing.T) { } } +func TestWithoutInitdata(t *testing.T) { + cfg := Config{ + fetchTimeout: 0, + digestPath: "", + initdataPath: "/does/not/exist", + parentPath: "", + writeFiles: nil, + initdataFiles: []string{}, + } + + err := extractInitdataAndHash(&cfg) + if err != nil { + t.Fatalf("extractInitdataAndHash returned err: %v", err) + } +} + func TestExtractInitdataWithMalicious(t *testing.T) { tempDir, _ := os.MkdirTemp("", "tmp_initdata_root") defer os.RemoveAll(tempDir)