-
Notifications
You must be signed in to change notification settings - Fork 35
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ( | ||
|
@@ -20,6 +22,7 @@ const ( | |
nfsVolumeNamePrefix = "nfs-volume" | ||
mountPath = "/ova" | ||
pvSize = "1Gi" | ||
qemuGroup = 107 | ||
) | ||
|
||
func (r Reconciler) CreateOVAServerDeployment(provider *api.Provider, ctx context.Context) { | ||
|
@@ -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") | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
There was a problem hiding this comment.
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