-
Notifications
You must be signed in to change notification settings - Fork 737
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor deployer(now controller) and kubernetes router
- Loading branch information
Showing
17 changed files
with
712 additions
and
439 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package canary | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/mitchellh/hashstructure" | ||
"github.com/weaveworks/flagger/pkg/apis/flagger/v1alpha3" | ||
clientset "github.com/weaveworks/flagger/pkg/client/clientset/versioned" | ||
"go.uber.org/zap" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
type Controller interface { | ||
Initialize(cd *v1alpha3.Canary, skipLivenessChecks bool) (label string, ports map[string]int32, err error) | ||
Promote(cd *v1alpha3.Canary) error | ||
HasTargetChanged(cd *v1alpha3.Canary) (bool, error) | ||
HasConfigChanged(cd *v1alpha3.Canary) (bool, error) | ||
Scale(cd *v1alpha3.Canary, replicas int32) error | ||
ScaleUp(cd *v1alpha3.Canary) error | ||
MakeStatusConditions(canaryStatus v1alpha3.CanaryStatus, | ||
phase v1alpha3.CanaryPhase) (bool, []v1alpha3.CanaryCondition) | ||
SyncStatus(cd *v1alpha3.Canary, status v1alpha3.CanaryStatus) error | ||
SetStatusFailedChecks(cd *v1alpha3.Canary, val int) error | ||
SetStatusWeight(cd *v1alpha3.Canary, val int) error | ||
SetStatusIterations(cd *v1alpha3.Canary, val int) error | ||
SetStatusPhase(cd *v1alpha3.Canary, phase v1alpha3.CanaryPhase) error | ||
IsPrimaryReady(cd *v1alpha3.Canary) (bool, error) | ||
IsCanaryReady(cd *v1alpha3.Canary) (bool, error) | ||
} | ||
|
||
type VersionKind string | ||
|
||
func NewVersionKind(ver, kind string) VersionKind { | ||
return VersionKind(fmt.Sprintf("%s/%s", ver, kind)) | ||
} | ||
|
||
var ( | ||
VersionKindDeployment = NewVersionKind("apps/v1", "Deployment") | ||
VersionKindService = NewVersionKind("core/v1", "Service") | ||
) | ||
|
||
func NewController(kubeClient kubernetes.Interface, flaggerClient clientset.Interface, logger *zap.SugaredLogger, | ||
labels []string) Controller { | ||
return &multiController{ | ||
deployers: map[VersionKind]Controller{ | ||
VersionKindDeployment: newDeploymentController(kubeClient, flaggerClient, logger, labels), | ||
VersionKindService: newServiceController(kubeClient, flaggerClient, logger), | ||
}, | ||
} | ||
} | ||
|
||
func newDeploymentController(kubeClient kubernetes.Interface, flaggerClient clientset.Interface, logger *zap.SugaredLogger, | ||
labels []string) Controller { | ||
return &DeploymentDeployer{ | ||
Logger: logger, | ||
KubeClient: kubeClient, | ||
FlaggerClient: flaggerClient, | ||
Labels: labels, | ||
ConfigTracker: ConfigTracker{ | ||
Logger: logger, | ||
KubeClient: kubeClient, | ||
FlaggerClient: flaggerClient, | ||
}, | ||
} | ||
} | ||
|
||
func newServiceController(kubeClient kubernetes.Interface, flaggerClient clientset.Interface, logger *zap.SugaredLogger) Controller { | ||
return &ServiceDeployer{ | ||
Logger: logger, | ||
KubeClient: kubeClient, | ||
FlaggerClient: flaggerClient, | ||
} | ||
} | ||
|
||
func hasSpecChanged(cd *v1alpha3.Canary, spec interface{}) (bool, error) { | ||
if cd.Status.LastAppliedSpec == "" { | ||
return true, nil | ||
} | ||
|
||
newHash, err := hashstructure.Hash(spec, nil) | ||
if err != nil { | ||
return false, fmt.Errorf("hash error %v", err) | ||
} | ||
|
||
// do not trigger a canary deployment on manual rollback | ||
if cd.Status.LastPromotedSpec == fmt.Sprintf("%d", newHash) { | ||
return false, nil | ||
} | ||
|
||
if cd.Status.LastAppliedSpec != fmt.Sprintf("%d", newHash) { | ||
return true, nil | ||
} | ||
|
||
return false, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.