Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[openfga] Configure cloud-sql-proxy sidecar #15692

Merged
merged 1 commit into from
Jan 12, 2023
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
2 changes: 2 additions & 0 deletions install/installer/pkg/components/openfga/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ const (
ImageTag = "v0.3.1"

ContainerName = "openfga"

CloudSQLProxyPort = 3306
)
186 changes: 121 additions & 65 deletions install/installer/pkg/components/openfga/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
package openfga

import (
"fmt"

"github.com/gitpod-io/gitpod/installer/pkg/cluster"
"github.com/gitpod-io/gitpod/installer/pkg/common"
"github.com/gitpod-io/gitpod/installer/pkg/components/database/cloudsql"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -20,6 +23,122 @@ import (
func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
labels := common.CustomizeLabel(ctx, Component, common.TypeMetaDeployment)

cfg := getExperimentalOpenFGAConfig(ctx)
if cfg == nil || !cfg.Enabled {
return nil, nil
}

containers := []corev1.Container{
{
Name: ContainerName,
Image: ctx.ImageName(common.ThirdPartyContainerRepo(ctx.Config.Repository, RegistryRepo), RegistryImage, ImageTag),
ImagePullPolicy: corev1.PullIfNotPresent,
Args: []string{
"run",
"--log-format=json",
"--log-level=warn",
},
Env: common.CustomizeEnvvar(ctx, Component, common.MergeEnv(
common.DefaultEnv(&ctx.Config),
)),
Ports: []corev1.ContainerPort{
{
ContainerPort: ContainerGRPCPort,
Name: ContainerGRPCName,
Protocol: *common.TCPProtocol,
},
{
ContainerPort: ContainerHTTPPort,
Name: ContainerHTTPName,
Protocol: *common.TCPProtocol,
},
{
ContainerPort: ContainerPlaygroundPort,
Name: ContainerPlaygroundName,
Protocol: *common.TCPProtocol,
},
},
Resources: common.ResourceRequirements(ctx, Component, ContainerName, corev1.ResourceRequirements{
Requests: corev1.ResourceList{
"cpu": resource.MustParse("1m"),
"memory": resource.MustParse("30Mi"),
},
Comment on lines +62 to +65
Copy link
Contributor

Choose a reason for hiding this comment

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

Where are these resource limits taken from, or are they an initial best guess?

Copy link
Member Author

Choose a reason for hiding this comment

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

An initial one, just picked something small. They are just requests (not limits) so it will realistically consume more

}),
SecurityContext: &corev1.SecurityContext{
RunAsGroup: pointer.Int64(65532),
RunAsNonRoot: pointer.Bool(true),
RunAsUser: pointer.Int64(65532),
},
LivenessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/healthz",
Port: intstr.IntOrString{IntVal: ContainerHTTPPort},
Scheme: corev1.URISchemeHTTP,
},
},
FailureThreshold: 3,
SuccessThreshold: 1,
TimeoutSeconds: 1,
},
ReadinessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/healthz",
Port: intstr.IntOrString{IntVal: ContainerHTTPPort},
Scheme: corev1.URISchemeHTTP,
},
},
FailureThreshold: 3,
SuccessThreshold: 1,
TimeoutSeconds: 1,
},
},
}

var volumes []corev1.Volume

if cfg.CloudSQL != nil {
containers = append(containers, corev1.Container{
Name: "cloud-sql-proxy",
SecurityContext: &corev1.SecurityContext{
Privileged: pointer.Bool(false),
RunAsNonRoot: pointer.Bool(false),
AllowPrivilegeEscalation: pointer.Bool(false),
},
Image: ctx.ImageName(cloudsql.ImageRepo, cloudsql.ImageName, cloudsql.ImageVersion),
Command: []string{
"/cloud_sql_proxy",
"-dir=/cloudsql",
fmt.Sprintf("-instances=%s=tcp:0.0.0.0:%d", cfg.CloudSQL.Instance, CloudSQLProxyPort),
"-credential_file=/credentials/credentials.json",
},
Ports: []corev1.ContainerPort{{
ContainerPort: CloudSQLProxyPort,
}},
VolumeMounts: []corev1.VolumeMount{{
MountPath: "/cloudsql",
Name: "cloudsql",
}, {
MountPath: "/credentials",
Name: "gcloud-sql-token",
}},
Env: common.CustomizeEnvvar(ctx, Component, []corev1.EnvVar{}),
})

volumes = append(volumes, []corev1.Volume{
{
Name: "cloudsql",
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
}, {
Name: "gcloud-sql-token",
VolumeSource: corev1.VolumeSource{Secret: &corev1.SecretVolumeSource{
SecretName: cfg.CloudSQL.ProxySecretRef,
}},
},
}...)
}

return []runtime.Object{
&appsv1.Deployment{
TypeMeta: common.TypeMetaDeployment,
Expand Down Expand Up @@ -51,71 +170,8 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
SecurityContext: &corev1.PodSecurityContext{
RunAsNonRoot: pointer.Bool(false),
},
Containers: []corev1.Container{{
Name: ContainerName,
Image: ctx.ImageName(common.ThirdPartyContainerRepo(ctx.Config.Repository, RegistryRepo), RegistryImage, ImageTag),
ImagePullPolicy: corev1.PullIfNotPresent,
Args: []string{
"run",
"--log-format=json",
"--log-level=warn",
},
Env: common.CustomizeEnvvar(ctx, Component, common.MergeEnv(
common.DefaultEnv(&ctx.Config),
)),
Ports: []corev1.ContainerPort{
{
ContainerPort: ContainerGRPCPort,
Name: ContainerGRPCName,
Protocol: *common.TCPProtocol,
},
{
ContainerPort: ContainerHTTPPort,
Name: ContainerHTTPName,
Protocol: *common.TCPProtocol,
},
{
ContainerPort: ContainerPlaygroundPort,
Name: ContainerPlaygroundName,
Protocol: *common.TCPProtocol,
},
},
Resources: common.ResourceRequirements(ctx, Component, ContainerName, corev1.ResourceRequirements{
Requests: corev1.ResourceList{
"cpu": resource.MustParse("1m"),
"memory": resource.MustParse("30Mi"),
},
}),
SecurityContext: &corev1.SecurityContext{
RunAsGroup: pointer.Int64(65532),
RunAsNonRoot: pointer.Bool(true),
RunAsUser: pointer.Int64(65532),
},
LivenessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/healthz",
Port: intstr.IntOrString{IntVal: ContainerHTTPPort},
Scheme: corev1.URISchemeHTTP,
},
},
FailureThreshold: 3,
SuccessThreshold: 1,
TimeoutSeconds: 1,
},
ReadinessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/healthz",
Port: intstr.IntOrString{IntVal: ContainerHTTPPort},
Scheme: corev1.URISchemeHTTP,
},
},
FailureThreshold: 3,
SuccessThreshold: 1,
TimeoutSeconds: 1,
},
}},
Containers: containers,
Volumes: volumes,
},
},
},
Expand Down
9 changes: 9 additions & 0 deletions install/installer/pkg/config/v1/experimental/experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@ type IAMConfig struct {

type OpenFGAConfig struct {
Enabled bool `json:"enabled"`

CloudSQL *struct {
Instance string `json:"instance"`
Database string `json:"database"`
// Credentials for CloudSQL proxy to authenticate with GCP
ProxySecretRef string `json:"proxySecretRef"`
// Username/Password to authenticate with the database
DatabaseSecretRef string `json:"databaseSecretRef"`
} `json:"cloudSql,omitempty"`
}

type WebAppConfig struct {
Expand Down