Skip to content

Commit

Permalink
Backport of [NET-6465] Respect connectInject.initContainer.resources …
Browse files Browse the repository at this point in the history
…for v1 API gateways into release/1.4.x (#3563)

* backport of commit 9d89a7c

* backport of commit 8a09d6e

* backport of commit 0890d59

---------

Co-authored-by: Nathan Coleman <nathan.coleman@hashicorp.com>
  • Loading branch information
1 parent 7e38df4 commit 77f2950
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changelog/3531.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
api-gateway: Apply `connectInject.initContainer.resources` to the init container for API gateway Pods.
```
4 changes: 4 additions & 0 deletions control-plane/api-gateway/common/helm_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package common
import (
"strings"
"time"

v1 "k8s.io/api/core/v1"
)

const componentAuthMethod = "k8s-component-auth-method"
Expand Down Expand Up @@ -40,6 +42,8 @@ type HelmConfig struct {
// MapPrivilegedServicePorts is the value which Consul will add to privileged container port values (ports < 1024)
// defined on a Gateway.
MapPrivilegedServicePorts int

InitContainerResources *v1.ResourceRequirements
}

type ConsulConfig struct {
Expand Down
38 changes: 35 additions & 3 deletions control-plane/api-gateway/gatekeeper/gatekeeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
corev1 "k8s.io/api/core/v1"
rbac "k8s.io/api/rbac/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -109,6 +110,16 @@ func TestUpsert(t *testing.T) {
},
helmConfig: common.HelmConfig{
ImageDataplane: dataplaneImage,
InitContainerResources: &corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceCPU: requireQuantity(t, "100m"),
corev1.ResourceMemory: requireQuantity(t, "2Gi"),
},
Requests: corev1.ResourceList{
corev1.ResourceCPU: requireQuantity(t, "100m"),
corev1.ResourceMemory: requireQuantity(t, "2Gi"),
},
},
},
initialResources: resources{},
finalResources: resources{
Expand Down Expand Up @@ -764,7 +775,7 @@ func TestUpsert(t *testing.T) {

err := gatekeeper.Upsert(context.Background(), tc.gateway, tc.gatewayClassConfig, tc.helmConfig)
require.NoError(t, err)
require.NoError(t, validateResourcesExist(t, client, tc.finalResources))
require.NoError(t, validateResourcesExist(t, client, tc.helmConfig, tc.finalResources))
})
}
}
Expand Down Expand Up @@ -953,7 +964,7 @@ func TestDelete(t *testing.T) {
Name: tc.gateway.Name,
})
require.NoError(t, err)
require.NoError(t, validateResourcesExist(t, client, tc.finalResources))
require.NoError(t, validateResourcesExist(t, client, tc.helmConfig, tc.finalResources))
require.NoError(t, validateResourcesAreDeleted(t, client, tc.initialResources))
})
}
Expand Down Expand Up @@ -983,7 +994,7 @@ func joinResources(resources resources) (objs []client.Object) {
return objs
}

func validateResourcesExist(t *testing.T, client client.Client, resources resources) error {
func validateResourcesExist(t *testing.T, client client.Client, helmConfig common.HelmConfig, resources resources) error {
t.Helper()

for _, expected := range resources.deployments {
Expand Down Expand Up @@ -1012,6 +1023,21 @@ func validateResourcesExist(t *testing.T, client client.Client, resources resour
require.Equal(t, expected.Spec.Template.ObjectMeta.Annotations, actual.Spec.Template.ObjectMeta.Annotations)
require.Equal(t, expected.Spec.Template.ObjectMeta.Labels, actual.Spec.Template.Labels)

// Ensure there is an init container
hasInitContainer := false
for _, container := range actual.Spec.Template.Spec.InitContainers {
if container.Name == injectInitContainerName {
hasInitContainer = true

// If the Helm config specifies init container resources, verify they are set
if helmConfig.InitContainerResources != nil {
assert.Equal(t, helmConfig.InitContainerResources.Limits, container.Resources.Limits)
assert.Equal(t, helmConfig.InitContainerResources.Requests, container.Resources.Requests)
}
}
}
assert.True(t, hasInitContainer)

// Ensure there is a consul-dataplane container dropping ALL capabilities, adding
// back the NET_BIND_SERVICE capability, and establishing a read-only root filesystem
hasDataplaneContainer := false
Expand Down Expand Up @@ -1349,3 +1375,9 @@ func configureServiceAccount(name, namespace string, labels map[string]string, r
},
}
}

func requireQuantity(t *testing.T, v string) resource.Quantity {
quantity, err := resource.ParseQuantity(v)
require.NoError(t, err)
return quantity
}
7 changes: 6 additions & 1 deletion control-plane/api-gateway/gatekeeper/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (

corev1 "k8s.io/api/core/v1"

"k8s.io/utils/pointer"

"github.com/hashicorp/consul-k8s/control-plane/api-gateway/common"
"github.com/hashicorp/consul-k8s/control-plane/connect-inject/constants"
"github.com/hashicorp/consul-k8s/control-plane/namespaces"
"k8s.io/utils/pointer"
)

const (
Expand Down Expand Up @@ -169,6 +170,10 @@ func initContainer(config common.HelmConfig, name, namespace string) (corev1.Con
})
}

if config.InitContainerResources != nil {
container.Resources = *config.InitContainerResources
}

// Openshift Assigns the security context for us, do not enable if it is enabled.
if !config.EnableOpenShift {
container.SecurityContext = &corev1.SecurityContext{
Expand Down
1 change: 1 addition & 0 deletions control-plane/subcommand/inject-connect/v1controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func (c *Command) configureV1Controllers(ctx context.Context, mgr manager.Manage
ConsulTLSServerName: c.consul.TLSServerName,
ConsulPartition: c.consul.Partition,
ConsulCACert: string(c.caCertPem),
InitContainerResources: &c.initContainerResources,
},
AllowK8sNamespacesSet: allowK8sNamespaces,
DenyK8sNamespacesSet: denyK8sNamespaces,
Expand Down

0 comments on commit 77f2950

Please sign in to comment.