Created
September 1, 2016 15:30
-
-
Save kozikow/be98a2b87255f64e87658916f40a46a3 to your computer and use it in GitHub Desktop.
Config generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"k8s.io/client-go/1.4/pkg/api/unversioned" | |
"k8s.io/client-go/1.4/pkg/api/v1" | |
"k8s.io/client-go/1.4/pkg/apis/extensions/v1beta1" | |
"k8s.io/client-go/1.4/pkg/runtime" | |
"k8s.io/client-go/1.4/pkg/runtime/serializer/json" | |
"k8s.io/client-go/1.4/pkg/util/intstr" | |
) | |
const ( | |
FlowerTier string = "flower" | |
WebServerTier string = "webserver" | |
SchedulerTier string = "scheduler" | |
WorkerTier string = "worker" | |
DockerImage string = "gcr.io/all-the-codes/docker_airflow:0.2" | |
Airflow string = "airflow" | |
HttpPort = 80 | |
WebContainerPort = 8000 | |
FlowerContainerPort = 5555 | |
) | |
func LoadBalancer(tier string, fromPort int32, toPort int, selector map[string]string) *v1.Service { | |
result := &v1.Service{ | |
Spec: v1.ServiceSpec{ | |
Type: "LoadBalancer", | |
Ports: []v1.ServicePort{{ | |
Protocol: "TCP", | |
Port: fromPort, | |
TargetPort: intstr.FromInt(toPort), | |
}}, | |
Selector: selector}, | |
ObjectMeta: v1.ObjectMeta{ | |
Name: tier, | |
Labels: map[string]string{ | |
"app": tier}}, | |
TypeMeta: unversioned.TypeMeta{ | |
Kind: "Service", | |
APIVersion: "v1"}, | |
} | |
return result | |
} | |
func AirflowContainer(tier string, exposedPorts []int32) v1.Container { | |
var ports = []v1.ContainerPort{} | |
for _, port := range exposedPorts { | |
ports = append(ports, v1.ContainerPort{ | |
ContainerPort: port}) | |
} | |
return v1.Container{ | |
Name: tier, | |
Image: DockerImage, | |
ImagePullPolicy: v1.PullAlways, | |
Env: []v1.EnvVar{ | |
v1.EnvVar{ | |
Name: "FLOWER_PORT", | |
Value: "5555"}, | |
v1.EnvVar{ | |
Name: "AIRFLOW_HOME", | |
Value: "/usr/local/airflow"}}, | |
Ports: ports, | |
Args: []string{tier}} | |
} | |
func AirflowDeployment(tier string, exposedPorts ...int32) *v1beta1.Deployment { | |
return &v1beta1.Deployment{ | |
ObjectMeta: v1.ObjectMeta{Name: tier}, | |
Spec: v1beta1.DeploymentSpec{ | |
Template: v1.PodTemplateSpec{ | |
Spec: v1.PodSpec{ | |
RestartPolicy: v1.RestartPolicyAlways, | |
Containers: []v1.Container{ | |
AirflowContainer(tier, exposedPorts)}}, | |
ObjectMeta: v1.ObjectMeta{ | |
Name: tier, | |
Labels: map[string]string{ | |
"app": Airflow, | |
"tier": tier}}}}, | |
TypeMeta: unversioned.TypeMeta{ | |
Kind: "Deployment", | |
APIVersion: "extensions/v1beta1"}} | |
} | |
func main() { | |
objects := []runtime.Object{ | |
AirflowDeployment(WebServerTier, WebContainerPort), | |
AirflowDeployment(FlowerTier, FlowerContainerPort), | |
AirflowDeployment(SchedulerTier), | |
AirflowDeployment(WorkerTier), | |
LoadBalancer(WebServerTier, HttpPort, WebContainerPort, | |
map[string]string{"app": Airflow, | |
"tier": WebServerTier}), | |
LoadBalancer(FlowerTier, HttpPort, FlowerContainerPort, | |
map[string]string{ | |
"app": Airflow, | |
"tier": FlowerTier})} | |
e := json.NewYAMLSerializer(json.DefaultMetaFactory, nil, nil) | |
for _, object := range objects { | |
err := e.Encode(object, os.Stdout) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("---") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment