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

Add support for ova provider creation for k8s in restricted namespace #643

Closed
wants to merge 1 commit into from
Closed
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 pkg/controller/provider/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ go_library(
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:meta",
"//vendor/k8s.io/apimachinery/pkg/util/intstr",
"//vendor/k8s.io/apiserver/pkg/storage/names",
"//vendor/k8s.io/client-go/kubernetes",
"//vendor/k8s.io/client-go/rest",
"//vendor/sigs.k8s.io/controller-runtime/pkg/client",
"//vendor/sigs.k8s.io/controller-runtime/pkg/controller",
"//vendor/sigs.k8s.io/controller-runtime/pkg/event",
Expand Down
107 changes: 79 additions & 28 deletions pkg/controller/provider/ova-setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)

const (
Expand All @@ -20,6 +22,7 @@ const (
nfsVolumeNamePrefix = "nfs-volume"
mountPath = "/ova"
pvSize = "1Gi"
qemuGroup = 107
)

func (r Reconciler) CreateOVAServerDeployment(provider *api.Provider, ctx context.Context) {
Expand Down Expand Up @@ -188,44 +191,92 @@ func (r *Reconciler) createServerService(provider *api.Provider, ctx context.Con
func (r *Reconciler) makeOvaProviderPodSpec(pvcName string, providerName string) core.PodSpec {
imageName, ok := os.LookupEnv(ovaImageVar)
if !ok {
r.Log.Error(nil, "Failed to find OVA server image")
fmt.Println("Failed to find OVA server image")
Copy link
Member

Choose a reason for hiding this comment

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

why? we need to use the logger here

return core.PodSpec{}
}

nfsVolumeName := fmt.Sprintf("%s-%s", nfsVolumeNamePrefix, providerName)
ovaContainerName := fmt.Sprintf("%s-pod-%s", ovaServer, providerName)
allowPrivilegeEscalation := false
nonRoot := true
user := int64(qemuGroup)
allowPrivilegeEscalation := false

commonSecurityContext := &core.SecurityContext{
AllowPrivilegeEscalation: &allowPrivilegeEscalation,
RunAsNonRoot: &nonRoot,
Capabilities: &core.Capabilities{
Drop: []core.Capability{"ALL"},
},
}

return core.PodSpec{
Containers: []core.Container{
container := core.Container{
Name: ovaContainerName,
Image: imageName,
Ports: []core.ContainerPort{{ContainerPort: 8080, Protocol: core.ProtocolTCP}},
VolumeMounts: []core.VolumeMount{
{
Name: ovaContainerName,
Ports: []core.ContainerPort{{ContainerPort: 8080, Protocol: core.ProtocolTCP}},
Image: imageName,
VolumeMounts: []core.VolumeMount{
{
Name: nfsVolumeName,
MountPath: mountPath,
},
},
SecurityContext: &core.SecurityContext{
AllowPrivilegeEscalation: &allowPrivilegeEscalation,
RunAsNonRoot: &nonRoot,
Capabilities: &core.Capabilities{
Drop: []core.Capability{"ALL"},
},
},
Name: nfsVolumeName,
MountPath: mountPath,
},
},
Volumes: []core.Volume{
{
Name: nfsVolumeName,
VolumeSource: core.VolumeSource{
PersistentVolumeClaim: &core.PersistentVolumeClaimVolumeSource{
ClaimName: pvcName,
},
},
SecurityContext: commonSecurityContext,
}

isOpenShift := r.runningInOpenShift()
//Security settings for k8s
if !isOpenShift {
container.SecurityContext.RunAsUser = &user
Copy link
Member

Choose a reason for hiding this comment

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

what happens when we set this on openshift?

}

volume := core.Volume{
Name: nfsVolumeName,
VolumeSource: core.VolumeSource{
PersistentVolumeClaim: &core.PersistentVolumeClaimVolumeSource{
ClaimName: pvcName,
},
},
}

podSpec := core.PodSpec{
Containers: []core.Container{container},
Volumes: []core.Volume{volume},
}

//Security pod settings for k8s
if !isOpenShift {
podSpec.SecurityContext = &core.PodSecurityContext{
Copy link
Member

Choose a reason for hiding this comment

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

same question here - I see that we set this and the RunAsUser regardless of the cluster type (openshift/k8s) on the pvc-init pod, why do we have to differentiate here?

FSGroup: &user,
SeccompProfile: &core.SeccompProfile{
Type: core.SeccompProfileTypeRuntimeDefault,
},
}
}
return podSpec
}

func (r *Reconciler) runningInOpenShift() bool {
Copy link
Member

Choose a reason for hiding this comment

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

there should be a "cleaner" way to know the cluster type (by propagating this from the operator to an environment variable of the pod) but please clarify first why we need to differentiate them here

config, err := rest.InClusterConfig()
if err != nil {
r.Log.Error(nil, "Failed to get cluster config")
return true
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
r.Log.Error(nil, "Failed create k8s clientSet")
return true
}

apiGroups, err := clientset.Discovery().ServerGroups()
if err != nil {
r.Log.Error(nil, "Failed to get server groups")
return true
}

for _, group := range apiGroups.Groups {
if group.Name == "route.openshift.io" {
return true
}
}
return false
}
Loading