Skip to content

Commit

Permalink
Merge pull request #154 from ArangoGutierrez/failure
Browse files Browse the repository at this point in the history
Don't remove the cache file during GHA
  • Loading branch information
ArangoGutierrez authored Aug 20, 2024
2 parents b15b45e + a757945 commit 63a9aa4
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 42 deletions.
6 changes: 6 additions & 0 deletions api/holodeck/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ const (
// ProviderSSH means the user already has a running instance
// and wants to use it as the infra provider via SSH
ProviderSSH Provider = "ssh"

// Possible values for the Conditions field
ConditionProgressing string = "Progressing"
ConditionDegraded string = "Degraded"
ConditionAvailable string = "Available"
ConditionTerminated string = "Terminated"
)

// Instance defines and AWS instance
Expand Down
10 changes: 8 additions & 2 deletions cmd/action/ci/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ func Run(log *logger.FunLogger) error {
}

if _, err := os.Stat(cachedir); err == nil {
if err := cleanup(log); err != nil {
return err
// Check if cache condition is Terminated
if ok, err := isTerminated(log); ok {
log.Info("Environment condition is Terminated no need to run Holodeck")
return nil
} else if err != nil {
if err := cleanup(log); err != nil {
return err
}
}
} else {
if err := entrypoint(log); err != nil {
Expand Down
35 changes: 31 additions & 4 deletions cmd/action/ci/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,38 @@ func cleanup(log *logger.FunLogger) error {
}
}

if err := os.RemoveAll(cachedir); err != nil {
log.Error(fmt.Errorf("error deleting cache directory: %s", err))
}

log.Info("Successfully deleted environment %s\n", cfg.Name)

return nil
}

func isTerminated(log *logger.FunLogger) (bool, error) {
log.Info("Checking for Terminated condition")

// Read the config file
configFile := os.Getenv("INPUT_HOLODECK_CONFIG")
if configFile == "" {
log.Error(fmt.Errorf("config file not provided"))
os.Exit(1)
}
configFile = "/github/workspace/" + configFile
cfg, err := jyaml.UnmarshalFromFile[v1alpha1.Environment](configFile)
if err != nil {
return false, fmt.Errorf("error reading config file: %s", err)
}

// Set env name
setCfgName(&cfg)

provider, err := newProvider(log, &cfg)
if err != nil {
return false, fmt.Errorf("failed to create provider: %v", err)
}

status, err := provider.Status()
if err != nil {
return false, fmt.Errorf("failed to get status: %v", err)
}

return status == "Terminated", nil
}
96 changes: 78 additions & 18 deletions pkg/provider/aws/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,22 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
ConditionProgressing string = "Progressing"
ConditionDegraded string = "Degraded"
ConditionAvailable string = "Available"
ConditionTerminated string = "Terminated"
)
func (p *Provider) Status() (string, error) {
// Read the cache file
data, err := os.ReadFile(p.cacheFile)
if err != nil {
return "", err
}

// Unmarshal the data into a v1alpha1.Environment object
var env v1alpha1.Environment
err = yaml.Unmarshal(data, &env)
if err != nil {
return "", err
}

return env.Status.Conditions[0].Type, nil
}

func (p *Provider) updateStatus(env v1alpha1.Environment, cache *AWS, condition []metav1.Condition) error {
// The actual 'env' object should *not* be modified when trying to
Expand All @@ -51,8 +61,8 @@ func (p *Provider) updateStatus(env v1alpha1.Environment, cache *AWS, condition
// Next step is to check if we need to update the status
modified := false

// Because there are only 3 possible conditions (degraded, available,
// and progressing), it isn't necessary to check if old
// Because there are only 4 possible conditions (degraded, available,
// progressing and Terminated), it isn't necessary to check if old
// conditions should be removed.
for _, newCondition := range envCopy.Status.Conditions {
oldCondition := meta.FindStatusCondition(env.Status.Conditions, newCondition.Type)
Expand Down Expand Up @@ -186,7 +196,7 @@ func (p *Provider) updateAvailableCondition(env v1alpha1.Environment, cache *AWS

// updateTerminatedCondition is used to mark a given resource as "terminated".
func (p *Provider) updateTerminatedCondition(env v1alpha1.Environment, cache *AWS) error {
terminatedCondition := getDegradedConditions("v1alpha1.Terminated", "AWS resources have been terminated")
terminatedCondition := getTerminatedConditions("v1alpha1.Terminated", "AWS resources have been terminated")
if err := p.updateStatus(env, cache, terminatedCondition); err != nil {
return err
}
Expand Down Expand Up @@ -220,17 +230,22 @@ func getAvailableConditions() []metav1.Condition {
now := time.Now()
return []metav1.Condition{
{
Type: ConditionAvailable,
Type: v1alpha1.ConditionAvailable,
Status: metav1.ConditionTrue,
LastTransitionTime: metav1.Time{Time: now},
},
{
Type: ConditionProgressing,
Type: v1alpha1.ConditionProgressing,
Status: metav1.ConditionFalse,
LastTransitionTime: metav1.Time{Time: now},
},
{
Type: v1alpha1.ConditionDegraded,
Status: metav1.ConditionFalse,
LastTransitionTime: metav1.Time{Time: now},
},
{
Type: ConditionDegraded,
Type: v1alpha1.ConditionTerminated,
Status: metav1.ConditionFalse,
LastTransitionTime: metav1.Time{Time: now},
},
Expand All @@ -244,22 +259,29 @@ func getDegradedConditions(reason string, message string) []metav1.Condition {
now := time.Now()
return []metav1.Condition{
{
Type: ConditionAvailable,
Type: v1alpha1.ConditionAvailable,
Status: metav1.ConditionFalse,
LastTransitionTime: metav1.Time{Time: now},
},
{
Type: ConditionProgressing,
Type: v1alpha1.ConditionProgressing,
Status: metav1.ConditionFalse,
LastTransitionTime: metav1.Time{Time: now},
},
{
Type: ConditionDegraded,
Type: v1alpha1.ConditionDegraded,
Status: metav1.ConditionTrue,
LastTransitionTime: metav1.Time{Time: now},
Reason: reason,
Message: message,
},
{
Type: v1alpha1.ConditionTerminated,
Status: metav1.ConditionFalse,
LastTransitionTime: metav1.Time{Time: now},
Reason: reason,
Message: message,
},
}
}

Expand All @@ -270,21 +292,59 @@ func getProgressingConditions(reason string, message string) []metav1.Condition
now := time.Now()
return []metav1.Condition{
{
Type: ConditionAvailable,
Type: v1alpha1.ConditionAvailable,
Status: metav1.ConditionFalse,
LastTransitionTime: metav1.Time{Time: now},
},
{
Type: ConditionProgressing,
Type: v1alpha1.ConditionProgressing,
Status: metav1.ConditionTrue,
LastTransitionTime: metav1.Time{Time: now},
Reason: reason,
Message: message,
},
{
Type: ConditionDegraded,
Type: v1alpha1.ConditionDegraded,
Status: metav1.ConditionFalse,
LastTransitionTime: metav1.Time{Time: now},
},
{
Type: v1alpha1.ConditionTerminated,
Status: metav1.ConditionFalse,
LastTransitionTime: metav1.Time{Time: now},
Reason: reason,
Message: message,
},
}
}

// getTerminatedConditions returns a list of conditions.Condition objects and marks
// every condition as FALSE except for conditions.ConditionTerminated so that the
// reconciler can determine that the resource is terminated.
func getTerminatedConditions(reason string, message string) []metav1.Condition {
now := time.Now()
return []metav1.Condition{
{
Type: v1alpha1.ConditionAvailable,
Status: metav1.ConditionFalse,
LastTransitionTime: metav1.Time{Time: now},
},
{
Type: v1alpha1.ConditionProgressing,
Status: metav1.ConditionFalse,
LastTransitionTime: metav1.Time{Time: now},
},
{
Type: v1alpha1.ConditionDegraded,
Status: metav1.ConditionFalse,
LastTransitionTime: metav1.Time{Time: now},
},
{
Type: v1alpha1.ConditionTerminated,
Status: metav1.ConditionTrue,
LastTransitionTime: metav1.Time{Time: now},
Reason: reason,
Message: message,
},
}
}
2 changes: 2 additions & 0 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type Provider interface {
Delete() error
// DryRun preflight checks
DryRun() error
// Status returns the status of the resources in the provider
Status() (string, error)

// Metada methods
UpdateResourcesTags(tags map[string]string, resources ...string) error
Expand Down
Loading

0 comments on commit 63a9aa4

Please sign in to comment.