Files
my-rook-config/pkg/operator/ceph/object/spec.go
T

215 lines
6.4 KiB
Go
Raw Normal View History

2018-10-09 21:43:24 -06:00
/*
Copyright 2018 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package object
import (
"fmt"
2019-07-23 07:43:10 +02:00
"github.com/rook/rook/pkg/operator/ceph/cluster/mon"
2019-02-14 09:16:21 -07:00
cephconfig "github.com/rook/rook/pkg/operator/ceph/config"
2018-10-09 21:43:24 -06:00
opspec "github.com/rook/rook/pkg/operator/ceph/spec"
"github.com/rook/rook/pkg/operator/k8sutil"
2019-02-04 19:35:45 +00:00
apps "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
2018-10-09 21:43:24 -06:00
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
2019-06-18 23:47:06 +02:00
func (c *clusterConfig) createDeployment(rgwConfig *rgwConfig) *apps.Deployment {
2019-06-13 13:00:22 +02:00
replicas := int32(1)
2019-02-04 19:35:45 +00:00
d := &apps.Deployment{
2018-10-09 21:43:24 -06:00
ObjectMeta: metav1.ObjectMeta{
2019-06-13 13:00:22 +02:00
Name: rgwConfig.ResourceName,
Namespace: c.store.Namespace,
2019-02-14 09:16:21 -07:00
Labels: c.getLabels(),
2018-10-09 21:43:24 -06:00
},
2019-02-04 19:35:45 +00:00
Spec: apps.DeploymentSpec{
Selector: &metav1.LabelSelector{
MatchLabels: c.getLabels(),
},
2019-06-13 13:00:22 +02:00
Template: c.makeRGWPodSpec(rgwConfig),
Replicas: &replicas,
2019-02-04 19:35:45 +00:00
Strategy: apps.DeploymentStrategy{
Type: apps.RecreateDeploymentStrategyType,
2018-12-08 00:10:45 +01:00
},
},
2018-10-09 21:43:24 -06:00
}
k8sutil.AddRookVersionLabelToDeployment(d)
c.store.Spec.Gateway.Annotations.ApplyToObjectMeta(&d.ObjectMeta)
opspec.AddCephVersionLabelToDeployment(c.clusterInfo.CephVersion, d)
k8sutil.SetOwnerRef(&d.ObjectMeta, &c.ownerRef)
2018-11-29 13:12:56 -07:00
2019-06-13 15:17:58 +02:00
return d
2018-10-09 21:43:24 -06:00
}
2019-06-13 13:00:22 +02:00
func (c *clusterConfig) makeRGWPodSpec(rgwConfig *rgwConfig) v1.PodTemplateSpec {
2018-10-09 21:43:24 -06:00
podSpec := v1.PodSpec{
2019-10-10 14:55:43 -06:00
InitContainers: []v1.Container{
c.makeChownInitContainer(rgwConfig),
},
2018-10-09 21:43:24 -06:00
Containers: []v1.Container{
2019-06-13 13:00:22 +02:00
c.makeDaemonContainer(rgwConfig),
2018-10-09 21:43:24 -06:00
},
RestartPolicy: v1.RestartPolicyAlways,
2019-02-14 09:16:21 -07:00
Volumes: append(
2019-06-13 13:00:22 +02:00
opspec.DaemonVolumes(c.DataPathMap, rgwConfig.ResourceName),
2019-02-14 09:16:21 -07:00
c.mimeTypesVolume(),
),
2019-08-04 15:40:29 -05:00
HostNetwork: c.clusterSpec.Network.IsHost(),
PriorityClassName: c.store.Spec.Gateway.PriorityClassName,
2018-10-09 21:43:24 -06:00
}
if c.clusterSpec.Network.IsHost() {
2018-10-09 21:43:24 -06:00
podSpec.DNSPolicy = v1.DNSClusterFirstWithHostNet
}
// Set the ssl cert if specified
if c.store.Spec.Gateway.SSLCertificateRef != "" {
2019-02-14 09:16:21 -07:00
// Keep the SSL secret as secure as possible in the container. Give only user read perms.
userReadOnly := int32(0400)
certVol := v1.Volume{
Name: certVolumeName,
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: c.store.Spec.Gateway.SSLCertificateRef,
Items: []v1.KeyToPath{
{Key: certKeyName, Path: certFilename, Mode: &userReadOnly},
}}}}
2018-10-09 21:43:24 -06:00
podSpec.Volumes = append(podSpec.Volumes, certVol)
}
c.store.Spec.Gateway.Placement.ApplyToPodSpec(&podSpec)
2018-10-09 21:43:24 -06:00
podTemplateSpec := v1.PodTemplateSpec{
2018-10-09 21:43:24 -06:00
ObjectMeta: metav1.ObjectMeta{
2019-06-13 13:00:22 +02:00
Name: rgwConfig.ResourceName,
Labels: c.getLabels(),
2018-10-09 21:43:24 -06:00
},
Spec: podSpec,
}
c.store.Spec.Gateway.Annotations.ApplyToObjectMeta(&podTemplateSpec.ObjectMeta)
return podTemplateSpec
2018-10-09 21:43:24 -06:00
}
2019-10-10 14:55:43 -06:00
func (c *clusterConfig) makeChownInitContainer(rgwConfig *rgwConfig) v1.Container {
return opspec.ChownCephDataDirsInitContainer(
*c.DataPathMap,
c.clusterSpec.CephVersion.Image,
opspec.DaemonVolumeMounts(c.DataPathMap, rgwConfig.ResourceName),
c.store.Spec.Gateway.Resources,
mon.PodSecurityContext(),
)
}
2019-06-13 13:00:22 +02:00
func (c *clusterConfig) makeDaemonContainer(rgwConfig *rgwConfig) v1.Container {
2018-10-09 21:43:24 -06:00
// start the rgw daemon in the foreground
container := v1.Container{
Name: "rgw",
Image: c.clusterSpec.CephVersion.Image,
2018-10-09 21:43:24 -06:00
Command: []string{
"radosgw",
},
2019-02-14 09:16:21 -07:00
Args: append(
append(
opspec.DaemonFlags(c.clusterInfo, c.store.Name),
2019-02-14 09:16:21 -07:00
"--foreground",
2019-06-13 13:00:22 +02:00
cephconfig.NewFlag("name", generateCephXUser(rgwConfig.ResourceName)),
2019-02-14 09:16:21 -07:00
cephconfig.NewFlag("host", opspec.ContainerEnvVarReference("POD_NAME")),
cephconfig.NewFlag("rgw-mime-types-file", mimeTypesMountPath()),
2019-11-15 21:56:25 +01:00
),
2019-02-14 09:16:21 -07:00
),
VolumeMounts: append(
2019-06-13 13:00:22 +02:00
opspec.DaemonVolumeMounts(c.DataPathMap, rgwConfig.ResourceName),
2019-02-14 09:16:21 -07:00
c.mimeTypesVolumeMount(),
),
Env: opspec.DaemonEnvVars(c.clusterSpec.CephVersion.Image),
2019-02-14 09:16:21 -07:00
Resources: c.store.Spec.Gateway.Resources,
2019-06-05 13:59:40 +02:00
LivenessProbe: &v1.Probe{
Handler: v1.Handler{
HTTPGet: &v1.HTTPGetAction{
Path: "/",
Port: intstr.FromInt(int(c.store.Spec.Gateway.Port)),
},
},
InitialDelaySeconds: 10,
},
2019-07-23 07:43:10 +02:00
SecurityContext: mon.PodSecurityContext(),
2018-10-09 21:43:24 -06:00
}
if c.store.Spec.Gateway.SSLCertificateRef != "" {
// Add a volume mount for the ssl certificate
2019-02-14 09:16:21 -07:00
mount := v1.VolumeMount{Name: certVolumeName, MountPath: certDir, ReadOnly: true}
container.VolumeMounts = append(container.VolumeMounts, mount)
}
2018-10-09 21:43:24 -06:00
return container
}
2019-02-14 09:16:21 -07:00
func (c *clusterConfig) startService() (string, error) {
labels := c.getLabels()
2018-10-09 21:43:24 -06:00
svc := &v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: c.instanceName(),
Namespace: c.store.Namespace,
2018-10-09 21:43:24 -06:00
Labels: labels,
},
Spec: v1.ServiceSpec{
Selector: labels,
},
}
k8sutil.SetOwnerRef(&svc.ObjectMeta, &c.ownerRef)
if c.clusterSpec.Network.IsHost() {
2018-10-09 21:43:24 -06:00
svc.Spec.ClusterIP = v1.ClusterIPNone
}
addPort(svc, "http", c.store.Spec.Gateway.Port)
addPort(svc, "https", c.store.Spec.Gateway.SecurePort)
2018-10-09 21:43:24 -06:00
svc, err := c.context.Clientset.CoreV1().Services(c.store.Namespace).Create(svc)
2018-10-09 21:43:24 -06:00
if err != nil {
if !errors.IsAlreadyExists(err) {
return "", fmt.Errorf("failed to create rgw service. %+v", err)
}
svc, err = c.context.Clientset.CoreV1().Services(c.store.Namespace).Get(c.instanceName(), metav1.GetOptions{})
2018-10-09 21:43:24 -06:00
if err != nil {
return "", fmt.Errorf("failed to get existing service IP. %+v", err)
}
return svc.Spec.ClusterIP, nil
}
logger.Infof("Gateway service running at %s:%d", svc.Spec.ClusterIP, c.store.Spec.Gateway.Port)
2018-10-09 21:43:24 -06:00
return svc.Spec.ClusterIP, nil
}
func addPort(service *v1.Service, name string, port int32) {
if port == 0 {
return
}
service.Spec.Ports = append(service.Spec.Ports, v1.ServicePort{
Name: name,
Port: port,
TargetPort: intstr.FromInt(int(port)),
Protocol: v1.ProtocolTCP,
})
}
2019-02-14 09:16:21 -07:00
func (c *clusterConfig) getLabels() map[string]string {
labels := opspec.PodLabels(AppName, c.store.Namespace, "rgw", c.store.Name)
labels["rook_object_store"] = c.store.Name
return labels
2018-10-09 21:43:24 -06:00
}