github.com/deemoprobe/k8s-first-commit@v0.0.0-20230430165612-a541f1982be3/pkg/kubelet/kubelet_server.go (about) 1 /* 2 Copyright 2014 Google Inc. All rights reserved. 3 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 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 package kubelet 17 18 import ( 19 "fmt" 20 "io/ioutil" 21 "net/http" 22 "net/url" 23 24 "github.com/GoogleCloudPlatform/kubernetes/pkg/api" 25 "gopkg.in/v1/yaml" 26 ) 27 28 type KubeletServer struct { 29 Kubelet *Kubelet 30 UpdateChannel chan api.ContainerManifest 31 } 32 33 func (s *KubeletServer) error(w http.ResponseWriter, err error) { 34 w.WriteHeader(http.StatusInternalServerError) 35 fmt.Fprintf(w, "Internal Error: %#v", err) 36 } 37 38 func (s *KubeletServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { 39 u, err := url.ParseRequestURI(req.RequestURI) 40 if err != nil { 41 s.error(w, err) 42 return 43 } 44 switch { 45 case u.Path == "/container": 46 defer req.Body.Close() 47 data, err := ioutil.ReadAll(req.Body) 48 if err != nil { 49 s.error(w, err) 50 return 51 } 52 var manifest api.ContainerManifest 53 err = yaml.Unmarshal(data, &manifest) 54 if err != nil { 55 s.error(w, err) 56 return 57 } 58 s.UpdateChannel <- manifest 59 case u.Path == "/containerInfo": 60 container := u.Query().Get("container") 61 if len(container) == 0 { 62 w.WriteHeader(http.StatusBadRequest) 63 fmt.Fprint(w, "Missing container query arg.") 64 return 65 } 66 id, err := s.Kubelet.GetContainerID(container) 67 body, err := s.Kubelet.GetContainerInfo(id) 68 if err != nil { 69 w.WriteHeader(http.StatusInternalServerError) 70 fmt.Fprintf(w, "Internal Error: %#v", err) 71 return 72 } 73 w.Header().Add("Content-type", "application/json") 74 w.WriteHeader(http.StatusOK) 75 fmt.Fprint(w, body) 76 default: 77 w.WriteHeader(http.StatusNotFound) 78 fmt.Fprint(w, "Not found.") 79 } 80 }