Skip to content

Commit

Permalink
Merge pull request #5 from amazeeio/versions
Browse files Browse the repository at this point in the history
Always clone the active version when adding domains
  • Loading branch information
shreddedbacon authored Mar 29, 2021
2 parents 8d5e44e + 22d3c79 commit 8b6b779
Showing 1 changed file with 52 additions and 70 deletions.
122 changes: 52 additions & 70 deletions controllers/ingress_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,62 +150,36 @@ func (r *IngressReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
}
// if we have any domains to add to the service, we do that here
if len(domainsToAdd) > 0 {
/*
//TODO: remove this section if the latest.Active check works
availableVersions, err := r.FastlyClient.ListVersions(&fastly.ListVersionsInput{
// if the latest version is active, then we should clone it
clonedVersion, err := r.FastlyClient.CloneVersion(
&fastly.CloneVersionInput{
Service: fastlyConfig.ServiceID,
Version: latest.Number,
})
for _, availableVersion := range availableVersions {
fmt.Println(availableVersion.Active)
}
*/

// use the latest version
clonedVersion := latest
// but if the latest version is active, then we want to clone it first
if latest.Active == true {
var err error
// if the latest version is active, then we should clone it
clonedVersion, err = r.FastlyClient.CloneVersion(
&fastly.CloneVersionInput{
Service: fastlyConfig.ServiceID,
Version: latest.Number,
})
if err != nil {
// @TODO: log the error and drop out, maybe do something else to help prevent cloning it again and again?
// check for existing non-activated versions?
opLog.Info(fmt.Sprintf("Unable to clone service version in fastly, pausing ingress, error was: %v", err))
patchErr := r.patchPausedStatus(ctx, ingress, fastlyConfig.ServiceID, fmt.Sprintf("%v", err), true)
if patchErr != nil {
// if we can't patch the resource, just log it and return
// next time it tries to reconcile, it will just exit here without doing anything else
opLog.Info(fmt.Sprintf("Unable to patch the ingress with paused status, giving up, error was: %v", patchErr))
}
return ctrl.Result{}, nil
if err != nil {
// @TODO: log the error and drop out, maybe do something else to help prevent cloning it again and again?
// check for existing non-activated versions?
opLog.Info(fmt.Sprintf("Unable to clone service version in fastly, pausing ingress, error was: %v", err))
patchErr := r.patchPausedStatus(ctx, ingress, fastlyConfig.ServiceID, fmt.Sprintf("%v", err), true)
if patchErr != nil {
// if we can't patch the resource, just log it and return
// next time it tries to reconcile, it will just exit here without doing anything else
opLog.Info(fmt.Sprintf("Unable to patch the ingress with paused status, giving up, error was: %v", patchErr))
}
opLog.Info(fmt.Sprintf(
"Cloned version %d of service %s",
clonedVersion.Number,
fastlyConfig.ServiceID,
))
return ctrl.Result{}, nil
}
opLog.Info(fmt.Sprintf(
"Cloned version %d of service %s",
clonedVersion.Number,
fastlyConfig.ServiceID,
))
// once we have the latest version, then we can update it
comment := fmt.Sprintf(
"Domains in ingress %s added by fastly-controller: cluster:%s:namespace:%s",
ingress.ObjectMeta.Name,
r.ClusterName,
ingress.ObjectMeta.Namespace,
)
if clonedVersion.Comment != "" {
// if there is already a comment, then add our comment to the end
comment = fmt.Sprintf(
"%s\nDomains in ingress %s added by fastly-controller: cluster:%s:namespace:%s",
clonedVersion.Comment,
ingress.ObjectMeta.Name,
r.ClusterName,
ingress.ObjectMeta.Namespace,
)
}
// update the version in fastly
version, err := r.FastlyClient.UpdateVersion(
&fastly.UpdateVersionInput{
Expand Down Expand Up @@ -423,28 +397,23 @@ func (r *IngressReconciler) deleteExternalResources(ctx context.Context,
))
// if there are any domains in the slice, then we need to clone the service and remove the domains from it
if len(delDomains) > 0 {
clonedVersion := latest
// but if the latest version is active, then we want to clone it first
if latest.Active == true {
var err error
// if the latest version is active, then we should clone it
clonedVersion, err = r.FastlyClient.CloneVersion(
&fastly.CloneVersionInput{
Service: fastlyConfig.ServiceID,
Version: latest.Number,
})
if err != nil {
// @TODO: log the error and drop out, maybe do something else to help prevent cloning it again and again?
// check for existing non-activated versions?
opLog.Info(fmt.Sprintf("Unable to clone service version in fastly, error was: %v", err))
return nil
}
opLog.Info(fmt.Sprintf(
"Cloned version %d of service %s",
clonedVersion.Number,
fastlyConfig.ServiceID,
))
// if the latest version is active, then we should clone it
clonedVersion, err := r.FastlyClient.CloneVersion(
&fastly.CloneVersionInput{
Service: fastlyConfig.ServiceID,
Version: latest.Number,
})
if err != nil {
// @TODO: log the error and drop out, maybe do something else to help prevent cloning it again and again?
// check for existing non-activated versions?
opLog.Info(fmt.Sprintf("Unable to clone service version in fastly, error was: %v", err))
return nil
}
opLog.Info(fmt.Sprintf(
"Cloned version %d of service %s",
clonedVersion.Number,
fastlyConfig.ServiceID,
))
comment := fmt.Sprintf(
"Domains in ingress %s removed by fastly-controller: cluster:%s:namespace:%s",
ingress.ObjectMeta.Name,
Expand Down Expand Up @@ -521,13 +490,24 @@ func (r *IngressReconciler) deleteExternalResources(ctx context.Context,
}

func (r *IngressReconciler) getLatestServiceDomains(fastlyConfig fastlyAPI) (*fastly.Version, []*fastly.Domain, error) {
latest, err := r.FastlyClient.LatestVersion(
&fastly.LatestVersionInput{
Service: fastlyConfig.ServiceID,
})
// get service information from fastly
service, err := r.FastlyClient.GetService(&fastly.GetServiceInput{
ID: fastlyConfig.ServiceID,
})
if err != nil {
return nil, nil, err
}

// iterate over the services to get the latest active version
latest := service.Versions[len(service.Versions)-1]
for _, version := range service.Versions {
if version.Active {
latest = version
break
}
}

// get all the domains from the active service
domains, err := r.FastlyClient.ListDomains(
&fastly.ListDomainsInput{
Service: fastlyConfig.ServiceID,
Expand All @@ -536,6 +516,8 @@ func (r *IngressReconciler) getLatestServiceDomains(fastlyConfig fastlyAPI) (*fa
if err != nil {
return nil, nil, err
}

// return these
return latest, domains, nil
}

Expand Down

0 comments on commit 8b6b779

Please sign in to comment.