1// +build !dockerless 2 3/* 4Copyright 2017 The Kubernetes Authors. 5 6Licensed under the Apache License, Version 2.0 (the "License"); 7you may not use this file except in compliance with the License. 8You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12Unless required by applicable law or agreed to in writing, software 13distributed under the License is distributed on an "AS IS" BASIS, 14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15See the License for the specific language governing permissions and 16limitations under the License. 17*/ 18 19package dockershim 20 21import ( 22 "encoding/json" 23 24 "k8s.io/kubernetes/pkg/kubelet/checkpointmanager" 25 "k8s.io/kubernetes/pkg/kubelet/checkpointmanager/checksum" 26) 27 28const ( 29 // default directory to store pod sandbox checkpoint files 30 sandboxCheckpointDir = "sandbox" 31 protocolTCP = Protocol("tcp") 32 protocolUDP = Protocol("udp") 33 protocolSCTP = Protocol("sctp") 34 schemaVersion = "v1" 35) 36 37// ContainerCheckpoint provides the interface for process container's checkpoint data 38type ContainerCheckpoint interface { 39 checkpointmanager.Checkpoint 40 GetData() (string, string, string, []*PortMapping, bool) 41} 42 43// Protocol is the type of port mapping protocol 44type Protocol string 45 46// PortMapping is the port mapping configurations of a sandbox. 47type PortMapping struct { 48 // Protocol of the port mapping. 49 Protocol *Protocol `json:"protocol,omitempty"` 50 // Port number within the container. 51 ContainerPort *int32 `json:"container_port,omitempty"` 52 // Port number on the host. 53 HostPort *int32 `json:"host_port,omitempty"` 54 // Host ip to expose. 55 HostIP string `json:"host_ip,omitempty"` 56} 57 58// CheckpointData contains all types of data that can be stored in the checkpoint. 59type CheckpointData struct { 60 PortMappings []*PortMapping `json:"port_mappings,omitempty"` 61 HostNetwork bool `json:"host_network,omitempty"` 62} 63 64// PodSandboxCheckpoint is the checkpoint structure for a sandbox 65type PodSandboxCheckpoint struct { 66 // Version of the pod sandbox checkpoint schema. 67 Version string `json:"version"` 68 // Pod name of the sandbox. Same as the pod name in the Pod ObjectMeta. 69 Name string `json:"name"` 70 // Pod namespace of the sandbox. Same as the pod namespace in the Pod ObjectMeta. 71 Namespace string `json:"namespace"` 72 // Data to checkpoint for pod sandbox. 73 Data *CheckpointData `json:"data,omitempty"` 74 // Checksum is calculated with fnv hash of the checkpoint object with checksum field set to be zero 75 Checksum checksum.Checksum `json:"checksum"` 76} 77 78// NewPodSandboxCheckpoint inits a PodSandboxCheckpoint with the given args 79func NewPodSandboxCheckpoint(namespace, name string, data *CheckpointData) ContainerCheckpoint { 80 return &PodSandboxCheckpoint{ 81 Version: schemaVersion, 82 Namespace: namespace, 83 Name: name, 84 Data: data, 85 } 86} 87 88// MarshalCheckpoint encodes the PodSandboxCheckpoint instance to a json object 89func (cp *PodSandboxCheckpoint) MarshalCheckpoint() ([]byte, error) { 90 cp.Checksum = checksum.New(*cp.Data) 91 return json.Marshal(*cp) 92} 93 94// UnmarshalCheckpoint decodes the blob data to the PodSandboxCheckpoint instance 95func (cp *PodSandboxCheckpoint) UnmarshalCheckpoint(blob []byte) error { 96 return json.Unmarshal(blob, cp) 97} 98 99// VerifyChecksum verifies whether the PodSandboxCheckpoint's data checksum is 100// the same as calculated checksum 101func (cp *PodSandboxCheckpoint) VerifyChecksum() error { 102 return cp.Checksum.Verify(*cp.Data) 103} 104 105// GetData gets the PodSandboxCheckpoint's version and some net information 106func (cp *PodSandboxCheckpoint) GetData() (string, string, string, []*PortMapping, bool) { 107 return cp.Version, cp.Name, cp.Namespace, cp.Data.PortMappings, cp.Data.HostNetwork 108} 109