Spinnaker第七節—Orca代碼詳解

這個春節假期由於新冠弄得一直在家憋着,閒得無事把spinnaker代碼翻出來把每個微服務重新review一遍,做了一些筆記,今天拿來分享一下,後續如果有spinnaker開發或者bug修復可以直接有的放矢。計劃先把核心的幾個微服務分幾期慢慢介紹下:orca、clouddriver、deck,今天先介紹orca。

Orca名詞解釋


     Orchestration
       與pipeline平行,我個人理解是orca這個微服務主要是爲了支持pipeline工作流的,爲了對接臨時性的任務包擴展出了ORCHESTRATION
     Execution
       一次執行。分爲兩種類型:PIPELINE和ORCHESTRATION,核心屬性數一個stage的集合
     Stage
       orca最複雜的類型,關聯一個execution和多個task,還維護有parentStage、downStreamStage、output等重要信息
     StageContext
       驅動這個階段的環境,提供組件步驟所需的輸入
     Task
       Stage的組成部分,orca中最小的操作單元     
    ExecutionRunner  Execution的執行者
    ExecutionRepository Execution的存儲者,我們的是redis,也承載着檢索查詢
    ExecutionLauncher Execution的啓動者,接收json封裝成Execution,進行存儲和啓動
      1 Json封裝Execution也會分爲PIPELINE和ORCHESTRATION兩種,本質沒區別
      2 最終啓動執行:

	    public Execution start(Execution execution) throws Exception {
			executionRunner.start(execution);
			return execution;
		}

orca-web子項目(入口)


  controller
    OperationsController: 與Execution相關的controller
      /orchestrate*
        最終startPipeline,以PIPELINE形式啓動execution
      /plan*
        不會真正執行,經過一系列校驗後通過ExecutionRepository存儲executor
      /ops*
        最終startTask,以ORCHESTRATION形式啓動executor
    TaskController: 
      /pipelines*
        對於pipeline的停、重、查,最終映射的對象類型還是execution
      /task*
        對於task的停、重、查,最終映射的對象類型還是execution
    orca-web總結:OperationsController和TaskController看似有重疊的部分都會涉及到execution,但是側重點不同。OperationsController側重於創建和啓動,TaskController側重於後續的暫停、撤銷和查詢。 

orca-core子項目(抽象層)


  根目錄:
    定義一系列的task:retryableTask、cancelableTask等
  annotations 自定義sync,用於標註同步,可以作爲自定義標註的學習參考
  clouddriver.utils 根據stage獲取各種clouddriver的信息
  commands 意義不大,不需要看
  config:orca配置
  exceptions:除開定義了異常,還定義了處理異常的Handler,借鑑下

/*
 * Copyright 2014 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netflix.spinnaker.orca.exceptions;

import java.util.Collections;
import java.util.Map;
import com.google.common.base.Throwables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import static java.lang.String.format;
import static org.springframework.core.Ordered.LOWEST_PRECEDENCE;

@Order(LOWEST_PRECEDENCE)
public class DefaultExceptionHandler implements ExceptionHandler {
  private final Logger log = LoggerFactory.getLogger(getClass());

  @Override public boolean handles(Exception e) {
    return true;
  }

  @Override
  public ExceptionHandler.Response handle(String taskName, Exception e) {
    Map<String, Object> exceptionDetails = ExceptionHandler.responseDetails("Unexpected Task Failure", Collections.singletonList(e.getMessage()));
    exceptionDetails.put("stackTrace", Throwables.getStackTraceAsString(e));
    log.warn(format("Error occurred during task %s", taskName), e);
    return new ExceptionHandler.Response(e.getClass().getSimpleName(), taskName, exceptionDetails, false);
}
}


  events:
    定義了execution、stage、task的各種事件,
    也定了以上事件的監聽基類,本質上沒有自己做處理,都是丟給delegate在處理,代理模式

  private void onTaskStarted(TaskStarted event) {
    Execution execution = retrieve(event);
    List<Stage> stages = execution.getStages();
    stages
      .stream()
      .filter(it -> it.getId().equals(event.getStageId()))
      .findFirst()
      .ifPresent(stage ->
        delegate.beforeTask(
          persister,
          stage,
          stage.getTasks().stream().filter(it -> it.getId().equals(event.getTaskId())).findFirst().get()
        )
      );
  }


  listeners:
    定義了ExecutionListener和StageListener兩個接口,也就是events中的delegate,定義了一些階段和要重寫的方法。像echo通知等就是這種實現機制。

/*
 * Copyright 2016 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.netflix.spinnaker.orca.listeners;

import com.netflix.spinnaker.orca.ExecutionStatus;
import com.netflix.spinnaker.orca.pipeline.model.Stage;
import com.netflix.spinnaker.orca.pipeline.model.Task;

public interface StageListener {
  default void beforeTask(Persister persister,
                                                Stage stage,
                                                Task task) {
    // do nothing
  }

  default void beforeStage(Persister persister,
                                                 Stage stage) {
    // do nothing
  }

  default void afterTask(Persister persister,
                                               Stage stage,
                                               Task task,
                                               ExecutionStatus executionStatus,
                                               boolean wasSuccessful) {
    // do nothing
  }

  default void afterStage(Persister persister,
                                                Stage stage) {
    // do nothing
  }
}


  locks
    看了下是爲了多節點部署而設計的,目前階段可以不關心
  pipeline
    最核心的一個類StageDefinitionBuilder,所有的stage都要繼承它
    AcquireLockStage、CheckPreconditionsStage、EvaluateVariablesStage、ReleaseLockStage、WaitStage 

orca-clouddriver子項目(核心實現)


    先搞明白3個服務:oort、mort和kato,這三個都是指向了clouddriver的restful
      oort是大頭:對接cluster、image、instance、loadbalance等雲平臺資源
      mort是小頭:對接vpc、安全組、賬號鑑權
      kkato是輔助:對接其它輔助的資源
    pipeline:
      stage對於task的組裝,注意其中的provider包是留給廠商自己私有化的,所以關於orca雲廠商差異化的部分也都集中在這裏。
    tasks:
      具體的task實現,也是未來集中關心的地方,provider中各個廠商都有自己的私有化 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章