Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Ciena Corporation |
| 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 model |
| 17 | |
| 18 | import ( |
| 19 | "fmt" |
| 20 | corev1 "k8s.io/api/core/v1" |
| 21 | "time" |
| 22 | ) |
| 23 | |
| 24 | type ComponentInstance struct { |
| 25 | Id string `json:"id"` |
| 26 | Namespace string `json:"namespace"` |
| 27 | Name string `json:"name"` |
| 28 | Ready string `json:"ready"` |
| 29 | Status string `json:"status"` |
| 30 | Restarts int `json:"restarts"` |
| 31 | Component string `json:"component"` |
| 32 | Version string `json:"version"` |
| 33 | StartTime string `json:"starttime"` |
| 34 | Age string `json:"age"` |
| 35 | } |
| 36 | |
| 37 | func (c *ComponentInstance) PopulateFrom(val corev1.Pod) { |
| 38 | c.Id = val.ObjectMeta.Name |
| 39 | c.Namespace = val.ObjectMeta.Namespace |
| 40 | c.Name = val.ObjectMeta.Labels["app.kubernetes.io/name"] |
| 41 | c.Component = val.ObjectMeta.Labels["app.kubernetes.io/component"] |
| 42 | c.Version = val.ObjectMeta.Labels["app.kubernetes.io/version"] |
| 43 | c.Status = string(val.Status.Phase) |
| 44 | |
| 45 | ready := 0 |
| 46 | var restarts int = 0 |
| 47 | for _, d := range val.Status.ContainerStatuses { |
| 48 | if d.Ready { |
| 49 | ready += 1 |
| 50 | } |
| 51 | restarts += int(d.RestartCount) |
| 52 | } |
| 53 | c.Ready = fmt.Sprintf("%d/%d", ready, len(val.Status.ContainerStatuses)) |
| 54 | c.Restarts = restarts |
| 55 | |
| 56 | c.StartTime = val.Status.StartTime.Time.String() |
| 57 | c.Age = time.Since(val.Status.StartTime.Time).Truncate(time.Second).String() |
| 58 | } |