github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/event/v2/state.go (about) 1 /* 2 Copyright 2021 The Skaffold Authors 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 17 package v2 18 19 import ( 20 "fmt" 21 22 pbuf "github.com/golang/protobuf/proto" 23 24 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants" 25 proto "github.com/GoogleContainerTools/skaffold/proto/v2" 26 ) 27 28 func GetState() (*proto.State, error) { 29 state := handler.getState() 30 return state, nil 31 } 32 33 func (ev *eventHandler) getState() *proto.State { 34 ev.stateLock.Lock() 35 // Deep copy 36 state := pbuf.Clone(ev.state).(*proto.State) 37 ev.stateLock.Unlock() 38 39 return state 40 } 41 42 func (ev *eventHandler) setState(state *proto.State) { 43 ev.stateLock.Lock() 44 ev.state = state 45 ev.stateLock.Unlock() 46 } 47 48 // InitializeState instantiates the global state of the skaffold runner, as well as the event log. 49 func InitializeState(cfg Config) { 50 handler.cfg = cfg 51 handler.setState(emptyState(cfg)) 52 } 53 54 func emptyState(cfg Config) *proto.State { 55 builds := map[string]string{} 56 for _, p := range cfg.GetPipelines() { 57 for _, a := range p.Build.Artifacts { 58 builds[a.ImageName] = NotStarted 59 } 60 } 61 metadata := initializeMetadata(cfg.GetPipelines(), cfg.GetKubeContext(), cfg.GetRunID()) 62 return emptyStateWithArtifacts(builds, metadata, cfg.AutoBuild(), cfg.AutoDeploy(), cfg.AutoSync()) 63 } 64 65 func emptyStateWithArtifacts(builds map[string]string, metadata *proto.Metadata, autoBuild, autoDeploy, autoSync bool) *proto.State { 66 return &proto.State{ 67 BuildState: &proto.BuildState{ 68 Artifacts: builds, 69 AutoTrigger: autoBuild, 70 StatusCode: proto.StatusCode_OK, 71 }, 72 TestState: &proto.TestState{ 73 Status: NotStarted, 74 StatusCode: proto.StatusCode_OK, 75 }, 76 RenderState: &proto.RenderState{ 77 Status: NotStarted, 78 StatusCode: proto.StatusCode_OK, 79 }, 80 DeployState: &proto.DeployState{ 81 Status: NotStarted, 82 AutoTrigger: autoDeploy, 83 StatusCode: proto.StatusCode_OK, 84 }, 85 StatusCheckState: emptyStatusCheckState(), 86 ForwardedPorts: make(map[int32]*proto.PortForwardEvent), 87 FileSyncState: &proto.FileSyncState{ 88 Status: NotStarted, 89 AutoTrigger: autoSync, 90 }, 91 Metadata: metadata, 92 } 93 } 94 95 func emptyStatusCheckState() *proto.StatusCheckState { 96 return &proto.StatusCheckState{ 97 Status: NotStarted, 98 Resources: map[string]string{}, 99 StatusCode: proto.StatusCode_OK, 100 } 101 } 102 103 // ResetStateOnBuild resets the build, test, deploy and sync state 104 func ResetStateOnBuild() { 105 builds := map[string]string{} 106 for k := range handler.getState().BuildState.Artifacts { 107 builds[k] = NotStarted 108 } 109 autoBuild, autoDeploy, autoSync := handler.getState().BuildState.AutoTrigger, handler.getState().DeployState.AutoTrigger, handler.getState().FileSyncState.AutoTrigger 110 newState := emptyStateWithArtifacts(builds, handler.getState().Metadata, autoBuild, autoDeploy, autoSync) 111 handler.setState(newState) 112 } 113 114 // ResetStateOnTest resets the test, deploy, sync and status check state 115 func ResetStateOnTest() { 116 newState := handler.getState() 117 newState.TestState.Status = NotStarted 118 handler.setState(newState) 119 } 120 121 // ResetStateOnDeploy resets the deploy, sync and status check state 122 func ResetStateOnDeploy() { 123 newState := handler.getState() 124 newState.DeployState.Status = NotStarted 125 newState.DeployState.StatusCode = proto.StatusCode_OK 126 newState.StatusCheckState = emptyStatusCheckState() 127 newState.ForwardedPorts = map[int32]*proto.PortForwardEvent{} 128 newState.DebuggingContainers = nil 129 handler.setState(newState) 130 } 131 132 func UpdateStateAutoBuildTrigger(t bool) { 133 newState := handler.getState() 134 newState.BuildState.AutoTrigger = t 135 handler.setState(newState) 136 } 137 138 func UpdateStateAutoDeployTrigger(t bool) { 139 newState := handler.getState() 140 newState.DeployState.AutoTrigger = t 141 handler.setState(newState) 142 } 143 144 func UpdateStateAutoSyncTrigger(t bool) { 145 newState := handler.getState() 146 newState.FileSyncState.AutoTrigger = t 147 handler.setState(newState) 148 } 149 150 func AutoTriggerDiff(phase constants.Phase, val bool) (bool, error) { 151 switch phase { 152 case constants.Build: 153 return val != handler.getState().BuildState.AutoTrigger, nil 154 case constants.Sync: 155 return val != handler.getState().FileSyncState.AutoTrigger, nil 156 case constants.Deploy: 157 return val != handler.getState().DeployState.AutoTrigger, nil 158 default: 159 return false, fmt.Errorf("unknown Phase %v not found in handler state", phase) 160 } 161 }