Skip to content
Merged
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
114 changes: 84 additions & 30 deletions launchpad/pkg/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ type FlagConfig struct {
} `json:"flags"`
}

const (
// /change writes only these two, the second paired with changing-flag's variant;
// RestoreChangingFlag relies on that.
ChangingFlagFile = "rawflags/changing-flag.json"
MetadataChangeFlagFile = "rawflags/selector-metadata-change-flags.json"
ChangingFlagKey = "changing-flag"
BaselineChangingVariant = "foo"
toggledChangingVariant = "bar"
)

var (
fileLock sync.Mutex // lock for file operations
changeLock sync.Mutex // lock for change requests (so that multiple requests don't overlap)
Expand All @@ -32,57 +42,100 @@ func ToggleChangingFlag() (string, error) {
changeLock.Lock()
defer changeLock.Unlock()

// Path to the configuration file
configFile := "rawflags/changing-flag.json"
current, err := readChangingVariant()
if err != nil {
return "", err
}

// Read the existing file
data, err := os.ReadFile(configFile)
next := BaselineChangingVariant
if current == BaselineChangingVariant {
next = toggledChangingVariant
}

return next, writeChangingVariant(next)
}

// RestoreChangingFlag flips changing-flag, and the metadata change fixture paired
// with it, back to their shipped state and reports whether it had to. The file is
// read, not remembered, so a restarted launchpad in a dirty container still
// restores; this is sound because writes wait for flagd to serve them.
func RestoreChangingFlag() (bool, error) {
changeLock.Lock()
defer changeLock.Unlock()

current, err := readChangingVariant()
if err != nil {
return false, err
}
if current == BaselineChangingVariant {
return false, nil
}

return true, writeChangingVariant(BaselineChangingVariant)
}

func readChangingVariant() (string, error) {
data, err := os.ReadFile(ChangingFlagFile)
if err != nil {
return "", err
}

// Parse the JSON into the FlagConfig struct
var config FlagConfig
if err := json.Unmarshal(data, &config); err != nil {
return "", err
}

// Find the "changing-flag" and toggle the default variant
flag, exists := config.Flags["changing-flag"]
flag, exists := config.Flags[ChangingFlagKey]
if !exists {
return "", errors.New("changing-flag not found in configuration")
}
return flag.DefaultVariant, nil
}

// writeChangingVariant returns once flagd serves the new variant, not just once
// our watcher has regenerated the merged file.
func writeChangingVariant(variant string) error {
// Read the existing file
data, err := os.ReadFile(ChangingFlagFile)
if err != nil {
return err
}

// Toggle the defaultVariant between "foo" and "bar"
if flag.DefaultVariant == "foo" {
flag.DefaultVariant = "bar"
} else {
flag.DefaultVariant = "foo"
// Parse the JSON into the FlagConfig struct
var config FlagConfig
if err := json.Unmarshal(data, &config); err != nil {
return err
}

// Find the "changing-flag" and set the default variant
flag, exists := config.Flags[ChangingFlagKey]
if !exists {
return errors.New("changing-flag not found in configuration")
}
flag.DefaultVariant = variant

// Save the updated flag back to the configuration
config.Flags["changing-flag"] = flag
config.Flags[ChangingFlagKey] = flag
// Serialize the updated configuration back to JSON
updatedData, err := json.MarshalIndent(config, "", " ")
if err != nil {
return "", err
return err
}

if err := writeAndWaitForUpdate(configFile, updatedData); err != nil {
return "", err
if err := writeAndWaitForUpdate(ChangingFlagFile, updatedData); err != nil {
return err
}
if err := toggleMetadataChangeFlags(); err != nil {
return "", err
if err := writeMetadataChangeVariant(variant); err != nil {
return err
}
return flag.DefaultVariant, nil
}

// toggleMetadataChangeFlags flips the flag set metadata version and the modified flag (default variant and metadata version);
// the overriding and inheriting flags are left untouched
func toggleMetadataChangeFlags() error {
configFile := "rawflags/selector-metadata-change-flags.json"
return awaitVariantInFlagd(variant)
}

data, err := os.ReadFile(configFile)
// writeMetadataChangeVariant sets the metadata change fixture to the state paired
// with changing-flag's variant, so restoring changing-flag restores it too.
func writeMetadataChangeVariant(variant string) error {
data, err := os.ReadFile(MetadataChangeFlagFile)
if err != nil {
return err
}
Expand All @@ -109,7 +162,7 @@ func toggleMetadataChangeFlags() error {
return errors.New("metadata-change-modified-flag metadata not found in configuration")
}

if setMetadata["version"] == "1" {
if variant == toggledChangingVariant {
setMetadata["version"] = "2"
modifiedFlag["defaultVariant"] = "bar"
modifiedFlagMetadata["version"] = "11"
Expand All @@ -124,10 +177,11 @@ func toggleMetadataChangeFlags() error {
return err
}

return writeAndWaitForUpdate(configFile, updatedData)
return writeAndWaitForUpdate(MetadataChangeFlagFile, updatedData)
}

// writeAndWaitForUpdate writes the file and blocks until the file watcher has picked it up
// writeAndWaitForUpdate writes the file and blocks until our file watcher has
// regenerated the merged flag file from it.
func writeAndWaitForUpdate(configFile string, updatedData []byte) error {
// the file watcher should be triggered instantly. If not, we add a timeout to prevent a hanging test
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(DefaultRestartTimeout)*time.Second)
Expand All @@ -154,7 +208,7 @@ func writeAndWaitForUpdate(configFile string, updatedData []byte) error {

<-ctx.Done()
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return fmt.Errorf("Flags were not updated in time: %v", ctx.Err())
return fmt.Errorf("flags were not updated in time: %v", ctx.Err())
}
return nil
}
Expand Down Expand Up @@ -190,7 +244,7 @@ func RestartFileWatcher() error {
fmt.Printf("%v config changed, regenerating JSON...\n", event.Name)
if err := CombineJSONFiles(InputDir); err != nil {
fmt.Printf("Error combining JSON files: %v\n", err)
return
continue
}
if strings.HasSuffix(event.Name, "changing-flag.json") || strings.HasSuffix(event.Name, "metadata-change-flags.json") {
for _, v := range changeFlagUpdateListeners {
Expand Down
Loading
Loading