Skip to content

[openfga] Configure CloudSQL datastore #15703

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

Merged
merged 1 commit into from
Jan 12, 2023
Merged
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
169 changes: 102 additions & 67 deletions install/installer/pkg/components/openfga/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,75 +28,10 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
return nil, nil
}

containers := []corev1.Container{
{
Copy link
Member Author

Choose a reason for hiding this comment

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

The definition is moved below. If there's config for CloudSQL, we configure the sidecar first and based on that we inject extra envs to the openfga application.

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,
},
},
}
var containers []corev1.Container

var volumes []corev1.Volume
var openfgaEnvVars []corev1.EnvVar

if cfg.CloudSQL != nil {
containers = append(containers, corev1.Container{
Expand Down Expand Up @@ -137,8 +72,108 @@ func deployment(ctx *common.RenderContext) ([]runtime.Object, error) {
}},
},
}...)

// We use our cloud-sql-proxy sidecar to target the DB.
dbHost := "localhost"
openfgaEnvVars = append(openfgaEnvVars, []corev1.EnvVar{
{
Name: "OPENFGA_DATASTORE_ENGINE",
Value: "mysql",
},
{
Name: "DB_PASSWORD",
ValueFrom: &corev1.EnvVarSource{SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: cfg.CloudSQL.DatabaseSecretRef,
},
Key: "password",
Copy link
Contributor

Choose a reason for hiding this comment

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

How will the real username and password be provided to the env var?

Copy link
Member Author

Choose a reason for hiding this comment

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

Through a k8s secret, which has been added in this PR https://github.com/gitpod-io/ops/pull/7712

TF loads the secret from GCP into k8s, we reference it through the cfg.CloudSQL.DatabaseSecretRef config

}},
},
{
Name: "DB_USERNAME",
ValueFrom: &corev1.EnvVarSource{SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: cfg.CloudSQL.DatabaseSecretRef,
},
Key: "user",
}},
},
{
Name: "OPENFGA_DATASTORE_URI",
Value: fmt.Sprintf("$(DB_USERNAME):$(DB_PASSWORD)@tcp(%s:%d)/%s?parseTime=true", dbHost, CloudSQLProxyPort, cfg.CloudSQL.Instance),
},
}...)
}
Comment on lines +78 to +106
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the core of this PRs change, rest are moves to satisfy declaration dependencies.


openfgaContainer := 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),
openfgaEnvVars,
)),
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 = append(containers, openfgaContainer)

return []runtime.Object{
&appsv1.Deployment{
TypeMeta: common.TypeMetaDeployment,
Expand Down