|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2022. Metacontroller authors. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package main |
| 17 | + |
| 18 | +import ( |
| 19 | + "io/ioutil" |
| 20 | + "log" |
| 21 | + "net/http" |
| 22 | + |
| 23 | + v1 "k8s.io/api/core/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/apimachinery/pkg/runtime" |
| 26 | + "k8s.io/apimachinery/pkg/util/json" |
| 27 | +) |
| 28 | + |
| 29 | +type Controller struct { |
| 30 | + metav1.TypeMeta `json:",inline"` |
| 31 | + metav1.ObjectMeta `json:"metadata"` |
| 32 | + Spec ControllerSpec `json:"spec"` |
| 33 | + Status ControllerStatus `json:"status"` |
| 34 | +} |
| 35 | + |
| 36 | +type ControllerSpec struct { |
| 37 | + Message string `json:"message"` |
| 38 | +} |
| 39 | + |
| 40 | +type ControllerStatus struct { |
| 41 | + Replicas int `json:"replicas"` |
| 42 | + Succeeded int `json:"succeeded"` |
| 43 | +} |
| 44 | + |
| 45 | +type SyncRequest struct { |
| 46 | + Parent Controller `json:"parent"` |
| 47 | + Children SyncRequestChildren `json:"children"` |
| 48 | +} |
| 49 | + |
| 50 | +type SyncRequestChildren struct { |
| 51 | + Pods map[string]*v1.Pod `json:"Pod.v1"` |
| 52 | +} |
| 53 | + |
| 54 | +type SyncResponse struct { |
| 55 | + Status ControllerStatus `json:"status"` |
| 56 | + Children []runtime.Object `json:"children"` |
| 57 | +} |
| 58 | + |
| 59 | +func sync(request *SyncRequest) (*SyncResponse, error) { |
| 60 | + response := &SyncResponse{} |
| 61 | + |
| 62 | + // Compute status based on latest observed state. |
| 63 | + for _, pod := range request.Children.Pods { |
| 64 | + response.Status.Replicas++ |
| 65 | + if pod.Status.Phase == v1.PodSucceeded { |
| 66 | + response.Status.Succeeded++ |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Generate desired children. |
| 71 | + pod := &v1.Pod{ |
| 72 | + TypeMeta: metav1.TypeMeta{ |
| 73 | + APIVersion: "v1", |
| 74 | + Kind: "Pod", |
| 75 | + }, |
| 76 | + ObjectMeta: metav1.ObjectMeta{ |
| 77 | + Name: request.Parent.Name, |
| 78 | + }, |
| 79 | + Spec: v1.PodSpec{ |
| 80 | + RestartPolicy: v1.RestartPolicyOnFailure, |
| 81 | + Containers: []v1.Container{ |
| 82 | + { |
| 83 | + Name: "hello", |
| 84 | + Image: "busybox", |
| 85 | + Command: []string{"echo", request.Parent.Spec.Message}, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | + response.Children = append(response.Children, pod) |
| 91 | + |
| 92 | + return response, nil |
| 93 | +} |
| 94 | + |
| 95 | +func syncHandler(w http.ResponseWriter, r *http.Request) { |
| 96 | + body, err := ioutil.ReadAll(r.Body) |
| 97 | + if err != nil { |
| 98 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 99 | + return |
| 100 | + } |
| 101 | + request := &SyncRequest{} |
| 102 | + if err := json.Unmarshal(body, request); err != nil { |
| 103 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 104 | + return |
| 105 | + } |
| 106 | + response, err := sync(request) |
| 107 | + if err != nil { |
| 108 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 109 | + return |
| 110 | + } |
| 111 | + body, err = json.Marshal(&response) |
| 112 | + if err != nil { |
| 113 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 114 | + return |
| 115 | + } |
| 116 | + w.Header().Set("Content-Type", "application/json") |
| 117 | + w.Write(body) |
| 118 | +} |
| 119 | + |
| 120 | +func main() { |
| 121 | + http.HandleFunc("/sync", syncHandler) |
| 122 | + |
| 123 | + log.Fatal(http.ListenAndServe(":8080", nil)) |
| 124 | +} |
0 commit comments