Skip to content
This repository was archived by the owner on Jun 24, 2020. It is now read-only.
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
1 change: 1 addition & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions config/300-serving-v1alpha1-knativeserving-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ spec:
name:
description: The name of the ConfigMap or Secret
type: string
high-availability:
description: Allows specification of HA control plane
type: object
properties:
replicas:
description: The number of replicas that HA parts of the control plane will be scaled to
type: integer
minimum: 1
type: object
status:
description: Status defines the observed state of KnativeServing
Expand Down
13 changes: 13 additions & 0 deletions pkg/apis/serving/v1alpha1/knativeserving_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ type CustomCerts struct {
Name string `json:"name"`
}

// HighAvailability specifies options for deploying Knative Serving control
// plane in a highly available manner. Note that HighAvailability is still in
// progress and does not currently provide a completely HA control plane.
type HighAvailability struct {
// Replicas is the number of replicas that HA parts of the control plane
// will be scaled to.
Replicas int32 `json:"replicas"`
}

// KnativeServingSpec defines the desired state of KnativeServing
// +k8s:openapi-gen=true
type KnativeServingSpec struct {
Expand All @@ -92,6 +101,10 @@ type KnativeServingSpec struct {

// Enables controller to trust registries with self-signed certificates
ControllerCustomCerts CustomCerts `json:"controller-custom-certs,omitempty"`

// Allows specification of HA control plane
// +optional
HighAvailability *HighAvailability `json:"high-availability,omitempty"`
}

// KnativeServingStatus defines the observed state of KnativeServing
Expand Down
21 changes: 21 additions & 0 deletions pkg/apis/serving/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/reconciler/knativeserving/common/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (platforms Platforms) Transformers(kubeClientSet kubernetes.Interface, inst
ImageTransform(instance, log),
GatewayTransform(instance, log),
CustomCertsTransform(instance, log),
HighAvailabilityTransform(instance, log),
}
for _, fn := range platforms {
transformer, err := fn(kubeClientSet, log)
Expand Down
74 changes: 74 additions & 0 deletions pkg/reconciler/knativeserving/common/ha.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright 2020 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package common

import (
mf "github.com/manifestival/manifestival"
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/sets"
servingv1alpha1 "knative.dev/serving-operator/pkg/apis/serving/v1alpha1"
)

const (
configMapName = "config-leader-election"
enabledComponentsKey = "enabledComponents"
componentsValue = "controller,hpaautoscaler,certcontroller,istiocontroller,nscontroller"
)

var deploymentNames = sets.NewString(
"controller",
"autoscaler-hpa",
"networking-certmanager",
"networking-ns-cert",
"networking-istio",
)

// HighAvailabilityTransform mutates configmaps and replicacounts of certain
// controllers when HA control plane is specified.
func HighAvailabilityTransform(instance *servingv1alpha1.KnativeServing, log *zap.SugaredLogger) mf.Transformer {
return func(u *unstructured.Unstructured) error {
if instance.Spec.HighAvailability == nil {
return nil
}
Comment on lines +45 to +47
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could put this outside the closure for a skosh more performance. Transform will ignore a nil Transformer

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather do this in a follow-up that changes everywhere that does this, since this PR is blocking the release


// Transform the leader election config.
if u.GetKind() == "ConfigMap" && u.GetName() == "config-leader-election" {
data, ok, err := unstructured.NestedStringMap(u.UnstructuredContent(), "data")
if err != nil {
return nil
}
if !ok {
data = map[string]string{}
}

data[enabledComponentsKey] = componentsValue
if err := unstructured.SetNestedStringMap(u.Object, data, "data"); err != nil {
return err
}
}

// Transform deployments that support HA.
if u.GetKind() == "Deployment" && deploymentNames.Has(u.GetName()) {
if err := unstructured.SetNestedField(u.Object, int64(instance.Spec.HighAvailability.Replicas), "spec", "replicas"); err != nil {
return err
}
}

return nil
}
}
150 changes: 150 additions & 0 deletions pkg/reconciler/knativeserving/common/ha_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
Copyright 2020 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package common

import (
"testing"

appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/kubernetes/scheme"

servingv1alpha1 "knative.dev/serving-operator/pkg/apis/serving/v1alpha1"
)

func TestHighAvailabilityTransform(t *testing.T) {
cases := []struct {
name string
config *servingv1alpha1.HighAvailability
in *unstructured.Unstructured
expected *unstructured.Unstructured
err error
}{
{
name: "No HA; ConfigMap",
config: nil,
in: makeUnstructuredConfigMap(t, nil),
expected: makeUnstructuredConfigMap(t, nil),
},
{
name: "HA; ConfigMap",
config: makeHa(2),
in: makeUnstructuredConfigMap(t, nil),
expected: makeUnstructuredConfigMap(t, map[string]string{
enabledComponentsKey: componentsValue,
}),
},
{
name: "HA; controller",
config: makeHa(2),
in: makeUnstructuredDeployment(t, "controller"),
expected: makeUnstructuredDeploymentReplicas(t, "controller", 2),
},
{
name: "HA; autoscaler-hpa",
config: makeHa(2),
in: makeUnstructuredDeployment(t, "autoscaler-hpa"),
expected: makeUnstructuredDeploymentReplicas(t, "autoscaler-hpa", 2),
},
{
name: "HA; networking-certmanager",
config: makeHa(2),
in: makeUnstructuredDeployment(t, "networking-certmanager"),
expected: makeUnstructuredDeploymentReplicas(t, "networking-certmanager", 2),
},
{
name: "HA; networking-ns-cert",
config: makeHa(2),
in: makeUnstructuredDeployment(t, "networking-ns-cert"),
expected: makeUnstructuredDeploymentReplicas(t, "networking-ns-cert", 2),
},
{
name: "HA; networking-istio",
config: makeHa(2),
in: makeUnstructuredDeployment(t, "networking-istio"),
expected: makeUnstructuredDeploymentReplicas(t, "networking-istio", 2),
},
{
name: "HA; some-unsupported-controller",
config: makeHa(2),
in: makeUnstructuredDeployment(t, "some-unsupported-controller"),
expected: makeUnstructuredDeployment(t, "some-unsupported-controller"),
},
}

for i := range cases {
tc := cases[i]

instance := &servingv1alpha1.KnativeServing{
Spec: servingv1alpha1.KnativeServingSpec{
HighAvailability: tc.config,
},
}

haTransform := HighAvailabilityTransform(instance, log)
err := haTransform(tc.in)

assertDeepEqual(t, err, tc.err)
assertDeepEqual(t, tc.in, tc.expected)
}
}

func makeHa(replicas int32) *servingv1alpha1.HighAvailability {
return &servingv1alpha1.HighAvailability{
Replicas: replicas,
}
}

func makeUnstructuredConfigMap(t *testing.T, data map[string]string) *unstructured.Unstructured {
cm := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: configMapName,
},
}
cm.Data = data
result := &unstructured.Unstructured{}
err := scheme.Scheme.Convert(cm, result, nil)
if err != nil {
t.Fatalf("Could not creat unstructured ConfigMap: %v, err: %v", cm, err)
}

return result
}

func makeUnstructuredDeployment(t *testing.T, name string) *unstructured.Unstructured {
return makeUnstructuredDeploymentReplicas(t, name, 1)
}

func makeUnstructuredDeploymentReplicas(t *testing.T, name string, replicas int32) *unstructured.Unstructured {
d := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Spec: appsv1.DeploymentSpec{
Replicas: &replicas,
},
}
result := &unstructured.Unstructured{}
err := scheme.Scheme.Convert(d, result, nil)
if err != nil {
t.Fatalf("Could not creat unstructured Deployment: %v, err: %v", d, err)
}

return result
}
2 changes: 1 addition & 1 deletion pkg/reconciler/knativeserving/common/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func updateDaemonSet(instance *servingv1alpha1.KnativeServing, u *unstructured.U

func updateRegistry(spec *corev1.PodSpec, instance *servingv1alpha1.KnativeServing, log *zap.SugaredLogger, name string) {
registry := instance.Spec.Registry
log.Debugw("Updating ", "name", name, "registry", registry)
log.Debugw("Updating", "name", name, "registry", registry)

updateImage(spec, &registry, log, name)
spec.ImagePullSecrets = addImagePullSecrets(
Expand Down