-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[usage] Initial installer configuration
- Loading branch information
Showing
9 changed files
with
269 additions
and
0 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
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
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,9 @@ | ||
// Copyright (c) 2021 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package usage | ||
|
||
const ( | ||
Component = "usage" | ||
) |
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,100 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the MIT License. See License-MIT.txt in the project root for license information. | ||
|
||
package usage | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gitpod-io/gitpod/common-go/baseserver" | ||
"github.com/gitpod-io/gitpod/installer/pkg/cluster" | ||
"github.com/gitpod-io/gitpod/installer/pkg/common" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
"k8s.io/utils/pointer" | ||
) | ||
|
||
func deployment(ctx *common.RenderContext) ([]runtime.Object, error) { | ||
labels := common.DefaultLabels(Component) | ||
return []runtime.Object{ | ||
&appsv1.Deployment{ | ||
TypeMeta: common.TypeMetaDeployment, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: Component, | ||
Namespace: ctx.Namespace, | ||
Labels: labels, | ||
}, | ||
Spec: appsv1.DeploymentSpec{ | ||
Selector: &metav1.LabelSelector{MatchLabels: labels}, | ||
Replicas: common.Replicas(ctx, Component), | ||
Strategy: common.DeploymentStrategy, | ||
Template: corev1.PodTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: Component, | ||
Namespace: ctx.Namespace, | ||
Labels: labels, | ||
}, | ||
Spec: corev1.PodSpec{ | ||
Affinity: common.NodeAffinity(cluster.AffinityLabelMeta), | ||
ServiceAccountName: Component, | ||
EnableServiceLinks: pointer.Bool(false), | ||
DNSPolicy: "ClusterFirst", | ||
RestartPolicy: "Always", | ||
TerminationGracePeriodSeconds: pointer.Int64(30), | ||
Containers: []corev1.Container{{ | ||
Name: Component, | ||
Image: ctx.ImageName(ctx.Config.Repository, Component, ctx.VersionManifest.Components.Usage.Version), | ||
Args: []string{ | ||
"run", | ||
}, | ||
ImagePullPolicy: corev1.PullIfNotPresent, | ||
Resources: common.ResourceRequirements(ctx, Component, Component, corev1.ResourceRequirements{ | ||
Requests: corev1.ResourceList{ | ||
"cpu": resource.MustParse("100m"), | ||
"memory": resource.MustParse("64Mi"), | ||
}, | ||
}), | ||
Ports: []corev1.ContainerPort{}, | ||
SecurityContext: &corev1.SecurityContext{ | ||
Privileged: pointer.Bool(false), | ||
}, | ||
Env: common.MergeEnv( | ||
common.DefaultEnv(&ctx.Config), | ||
), | ||
LivenessProbe: &corev1.Probe{ | ||
ProbeHandler: corev1.ProbeHandler{ | ||
HTTPGet: &corev1.HTTPGetAction{ | ||
Path: "/live", | ||
Port: intstr.IntOrString{IntVal: baseserver.BuiltinHealthPort}, | ||
Scheme: corev1.URISchemeHTTP, | ||
}, | ||
}, | ||
FailureThreshold: 3, | ||
SuccessThreshold: 1, | ||
TimeoutSeconds: 1, | ||
}, | ||
ReadinessProbe: &corev1.Probe{ | ||
ProbeHandler: corev1.ProbeHandler{ | ||
HTTPGet: &corev1.HTTPGetAction{ | ||
Path: "/ready", | ||
Port: intstr.IntOrString{IntVal: baseserver.BuiltinHealthPort}, | ||
Scheme: corev1.URISchemeHTTP, | ||
}, | ||
}, | ||
FailureThreshold: 3, | ||
SuccessThreshold: 1, | ||
TimeoutSeconds: 1, | ||
}, | ||
}, | ||
*common.KubeRBACProxyContainerWithConfig(ctx, 9500, fmt.Sprintf("http://127.0.0.1:%d/", baseserver.BuiltinMetricsPort)), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the MIT License. See License-MIT.txt in the project root for license information. | ||
|
||
package usage | ||
|
||
import ( | ||
"github.com/gitpod-io/gitpod/common-go/log" | ||
"github.com/gitpod-io/gitpod/installer/pkg/common" | ||
"github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
func Objects(ctx *common.RenderContext) ([]runtime.Object, error) { | ||
cfg := getExperimentalConfig(ctx) | ||
if cfg == nil { | ||
return nil, nil | ||
} | ||
|
||
log.Debug("Detected experimental.WebApp.Usage configuration", cfg) | ||
return common.CompositeRenderFunc( | ||
deployment, | ||
rolebinding, | ||
common.DefaultServiceAccount(Component), | ||
)(ctx) | ||
} | ||
|
||
func getExperimentalConfig(ctx *common.RenderContext) *experimental.UsageConfig { | ||
var experimentalCfg *experimental.Config | ||
|
||
_ = ctx.WithExperimental(func(ucfg *experimental.Config) error { | ||
experimentalCfg = ucfg | ||
return nil | ||
}) | ||
|
||
if experimentalCfg == nil || experimentalCfg.WebApp == nil || experimentalCfg.WebApp.Usage == nil { | ||
return nil | ||
} | ||
|
||
return experimentalCfg.WebApp.Usage | ||
} |
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,51 @@ | ||
// Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
// Licensed under the MIT License. See License-MIT.txt in the project root for license information. | ||
|
||
package usage | ||
|
||
import ( | ||
"github.com/gitpod-io/gitpod/installer/pkg/common" | ||
"github.com/gitpod-io/gitpod/installer/pkg/config/v1" | ||
"github.com/gitpod-io/gitpod/installer/pkg/config/v1/experimental" | ||
"github.com/gitpod-io/gitpod/installer/pkg/config/versions" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestObjects_NotRenderedByDefault(t *testing.T) { | ||
ctx, err := common.NewRenderContext(config.Config{}, versions.Manifest{}, "test-namespace") | ||
require.NoError(t, err) | ||
|
||
objects, err := Objects(ctx) | ||
require.NoError(t, err) | ||
require.Empty(t, objects, "no objects should be rendered with default config") | ||
} | ||
|
||
func TestObjects_RenderedWhenExperimentalConfigSet(t *testing.T) { | ||
ctx := renderContextWithUsageEnabled(t) | ||
|
||
objects, err := Objects(ctx) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, objects, "must render objects because experimental config is specified") | ||
require.Len(t, objects, 4, "should render expected k8s objects") | ||
} | ||
|
||
func renderContextWithUsageEnabled(t *testing.T) *common.RenderContext { | ||
ctx, err := common.NewRenderContext(config.Config{ | ||
Domain: "test.domain.everything.awesome.is", | ||
Experimental: &experimental.Config{ | ||
WebApp: &experimental.WebAppConfig{ | ||
Usage: &experimental.UsageConfig{Enabled: true}, | ||
}, | ||
}, | ||
}, versions.Manifest{ | ||
Components: versions.Components{ | ||
Usage: versions.Versioned{ | ||
Version: "commit-test-latest", | ||
}, | ||
}, | ||
}, "test-namespace") | ||
require.NoError(t, err) | ||
|
||
return ctx | ||
} |
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,56 @@ | ||
// Copyright (c) 2021 Gitpod GmbH. All rights reserved. | ||
// Licensed under the GNU Affero General Public License (AGPL). | ||
// See License-AGPL.txt in the project root for license information. | ||
|
||
package usage | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gitpod-io/gitpod/installer/pkg/common" | ||
|
||
rbacv1 "k8s.io/api/rbac/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
func rolebinding(ctx *common.RenderContext) ([]runtime.Object, error) { | ||
labels := common.DefaultLabels(Component) | ||
|
||
return []runtime.Object{ | ||
&rbacv1.ClusterRoleBinding{ | ||
TypeMeta: common.TypeMetaClusterRoleBinding, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: fmt.Sprintf("%s-%s-rb-kube-rbac-proxy", ctx.Namespace, Component), | ||
Labels: labels, | ||
}, | ||
RoleRef: rbacv1.RoleRef{ | ||
Kind: "ClusterRole", | ||
Name: fmt.Sprintf("%s-kube-rbac-proxy", ctx.Namespace), | ||
APIGroup: "rbac.authorization.k8s.io", | ||
}, | ||
Subjects: []rbacv1.Subject{{ | ||
Kind: "ServiceAccount", | ||
Name: Component, | ||
Namespace: ctx.Namespace, | ||
}}, | ||
}, | ||
&rbacv1.RoleBinding{ | ||
TypeMeta: common.TypeMetaRoleBinding, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: Component, | ||
Namespace: ctx.Namespace, | ||
Labels: common.DefaultLabels(Component), | ||
}, | ||
RoleRef: rbacv1.RoleRef{ | ||
Kind: "ClusterRole", | ||
Name: fmt.Sprintf("%s-ns-psp:restricted-root-user", ctx.Namespace), | ||
APIGroup: "rbac.authorization.k8s.io", | ||
}, | ||
Subjects: []rbacv1.Subject{{ | ||
Kind: "ServiceAccount", | ||
Name: Component, | ||
}}, | ||
}, | ||
}, 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
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