Skip to content

Commit

Permalink
initdata: don't fail process-user-data when absent
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
mkulke committed Aug 9, 2024
1 parent 8f91e2c commit 50255a7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/cloud-api-adaptor/pkg/userdata/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -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)
}
Expand Down
16 changes: 16 additions & 0 deletions src/cloud-api-adaptor/pkg/userdata/provision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 50255a7

Please sign in to comment.