github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/server/v2/endpoints.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  	"context"
    21  
    22  	"github.com/golang/protobuf/ptypes/empty"
    23  	"google.golang.org/grpc/codes"
    24  	"google.golang.org/grpc/status"
    25  
    26  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
    27  	event "github.com/GoogleContainerTools/skaffold/pkg/skaffold/event/v2"
    28  	proto "github.com/GoogleContainerTools/skaffold/proto/v2"
    29  )
    30  
    31  var (
    32  	// For Testing
    33  	resetStateOnBuild  = event.ResetStateOnBuild
    34  	resetStateOnDeploy = event.ResetStateOnDeploy
    35  )
    36  
    37  func (s *Server) GetState(context.Context, *empty.Empty) (*proto.State, error) {
    38  	return event.GetState()
    39  }
    40  
    41  func (s *Server) Events(_ *empty.Empty, stream proto.SkaffoldV2Service_EventsServer) error {
    42  	return event.ForEachEvent(stream.Send)
    43  }
    44  
    45  func (s *Server) ApplicationLogs(_ *empty.Empty, stream proto.SkaffoldV2Service_ApplicationLogsServer) error {
    46  	return event.ForEachApplicationLog(stream.Send)
    47  }
    48  
    49  func (s *Server) Handle(ctx context.Context, e *proto.Event) (*empty.Empty, error) {
    50  	return &empty.Empty{}, event.Handle(e)
    51  }
    52  
    53  func (s *Server) Execute(ctx context.Context, request *proto.UserIntentRequest) (*empty.Empty, error) {
    54  	intent := request.GetIntent()
    55  	if intent.GetDevloop() {
    56  		resetStateOnBuild()
    57  		go func() {
    58  			s.BuildIntentCallback()
    59  		}()
    60  		return &empty.Empty{}, nil
    61  	}
    62  
    63  	if intent.GetBuild() {
    64  		resetStateOnBuild()
    65  		go func() {
    66  			s.BuildIntentCallback()
    67  		}()
    68  	}
    69  
    70  	if intent.GetDeploy() {
    71  		resetStateOnDeploy()
    72  		go func() {
    73  			s.DeployIntentCallback()
    74  		}()
    75  	}
    76  
    77  	if intent.GetSync() {
    78  		go func() {
    79  			s.SyncIntentCallback()
    80  		}()
    81  	}
    82  
    83  	return &empty.Empty{}, nil
    84  }
    85  
    86  func (s *Server) AutoBuild(ctx context.Context, request *proto.TriggerRequest) (res *empty.Empty, err error) {
    87  	return executeAutoTrigger(constants.Build, request, event.UpdateStateAutoBuildTrigger, event.ResetStateOnBuild, s.AutoBuildCallback)
    88  }
    89  
    90  func (s *Server) AutoDeploy(ctx context.Context, request *proto.TriggerRequest) (res *empty.Empty, err error) {
    91  	return executeAutoTrigger(constants.Deploy, request, event.UpdateStateAutoDeployTrigger, event.ResetStateOnDeploy, s.AutoDeployCallback)
    92  }
    93  
    94  func (s *Server) AutoSync(ctx context.Context, request *proto.TriggerRequest) (res *empty.Empty, err error) {
    95  	return executeAutoTrigger(constants.Sync, request, event.UpdateStateAutoSyncTrigger, func() {}, s.AutoSyncCallback)
    96  }
    97  
    98  func executeAutoTrigger(triggerName constants.Phase, request *proto.TriggerRequest, updateTriggerStateFunc func(bool), resetPhaseStateFunc func(), serverCallback func(bool)) (res *empty.Empty, err error) {
    99  	res = &empty.Empty{}
   100  
   101  	trigger := request.GetState().GetEnabled()
   102  	update, err := event.AutoTriggerDiff(triggerName, trigger)
   103  	if err != nil {
   104  		return
   105  	}
   106  	if !update {
   107  		err = status.Errorf(codes.AlreadyExists, "auto %v is already set to %t", triggerName, trigger)
   108  		return
   109  	}
   110  	// update trigger state
   111  	updateTriggerStateFunc(trigger)
   112  	if trigger {
   113  		// reset phase state only when auto trigger is being set to true
   114  		resetPhaseStateFunc()
   115  	}
   116  	go func() {
   117  		serverCallback(trigger)
   118  	}()
   119  	return
   120  }