mirror of
https://github.com/hanzozt/zt.git
synced 2026-11-03 05:01:58 +00:00
160 lines
4.6 KiB
Go
160 lines
4.6 KiB
Go
/*
|
|
Copyright NetFoundry 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
|
|
|
|
https://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 fabric
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/michaelquigley/pfxlog"
|
|
"github.com/openziti/channel/v4"
|
|
"github.com/openziti/channel/v4/protobufs"
|
|
"github.com/openziti/ziti/common/pb/mgmt_pb"
|
|
"github.com/openziti/ziti/ziti/cmd/api"
|
|
"github.com/openziti/ziti/ziti/cmd/common"
|
|
"github.com/spf13/cobra"
|
|
"google.golang.org/protobuf/proto"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
type validateCircuitsAction struct {
|
|
api.Options
|
|
|
|
includeValidCircuits bool
|
|
includeValidRouters bool
|
|
|
|
eventNotify chan *mgmt_pb.RouterCircuitDetails
|
|
}
|
|
|
|
func NewValidateCircuitsCmd(p common.OptionsProvider) *cobra.Command {
|
|
action := validateCircuitsAction{
|
|
Options: api.Options{
|
|
CommonOptions: p(),
|
|
},
|
|
}
|
|
|
|
validateCircuitsCmd := &cobra.Command{
|
|
Use: "circuits <router filter>",
|
|
Short: "Validate circuits",
|
|
Example: "ziti fabric validate circuits --filter 'name=\"my-router\"'",
|
|
Args: cobra.MaximumNArgs(1),
|
|
RunE: action.validateRouterCircuits,
|
|
}
|
|
|
|
action.AddCommonFlags(validateCircuitsCmd)
|
|
validateCircuitsCmd.Flags().BoolVar(&action.includeValidCircuits, "include-valid", false, "Don't hide results for valid circuits")
|
|
validateCircuitsCmd.Flags().BoolVar(&action.includeValidRouters, "include-valid-routers", false, "Don't hide results for valid routers")
|
|
return validateCircuitsCmd
|
|
}
|
|
|
|
func (self *validateCircuitsAction) validateRouterCircuits(_ *cobra.Command, args []string) error {
|
|
closeNotify := make(chan struct{})
|
|
self.eventNotify = make(chan *mgmt_pb.RouterCircuitDetails, 1)
|
|
|
|
bindHandler := func(binding channel.Binding) error {
|
|
binding.AddReceiveHandler(int32(mgmt_pb.ContentType_ValidateCircuitsResultType), self)
|
|
binding.AddCloseHandler(channel.CloseHandlerF(func(ch channel.Channel) {
|
|
close(closeNotify)
|
|
}))
|
|
return nil
|
|
}
|
|
|
|
ch, err := api.NewWsMgmtChannel(channel.BindHandlerF(bindHandler))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
filter := ""
|
|
if len(args) > 0 {
|
|
filter = args[0]
|
|
}
|
|
|
|
request := &mgmt_pb.ValidateCircuitsRequest{
|
|
RouterFilter: filter,
|
|
}
|
|
|
|
responseMsg, err := protobufs.MarshalTyped(request).WithTimeout(time.Duration(self.Timeout) * time.Second).SendForReply(ch)
|
|
|
|
response := &mgmt_pb.ValidateCircuitsResponse{}
|
|
if err = protobufs.TypedResponse(response).Unmarshall(responseMsg, err); err != nil {
|
|
return err
|
|
}
|
|
|
|
if !response.Success {
|
|
return fmt.Errorf("failed to start circuit validation: %s", response.Message)
|
|
}
|
|
|
|
fmt.Printf("started validation of %v routers\n", response.RouterCount)
|
|
|
|
expected := response.RouterCount
|
|
|
|
errCount := 0
|
|
for expected > 0 {
|
|
select {
|
|
case <-closeNotify:
|
|
fmt.Printf("channel closed, exiting")
|
|
return nil
|
|
case routerDetail := <-self.eventNotify:
|
|
result := "validation successful"
|
|
if !routerDetail.ValidateSuccess {
|
|
result = fmt.Sprintf("error: unable to validate (%s)", routerDetail.Message)
|
|
errCount++
|
|
}
|
|
|
|
routerHeaderDone := false
|
|
outputRouterHeader := func() {
|
|
fmt.Printf("routerId: %s, routerName: %v, circuits: %v, %s\n",
|
|
routerDetail.RouterId, routerDetail.RouterName, len(routerDetail.Details), result)
|
|
routerHeaderDone = true
|
|
}
|
|
|
|
if self.includeValidRouters || !routerDetail.ValidateSuccess {
|
|
outputRouterHeader()
|
|
}
|
|
|
|
for _, circuitDetail := range routerDetail.Details {
|
|
if self.includeValidCircuits || circuitDetail.IsInErrorState() {
|
|
if !routerHeaderDone {
|
|
outputRouterHeader()
|
|
}
|
|
fmt.Printf("\tcircuitId: %s, missing in ctrl: %v, missing in forwarder: %v, missing in edge: %v, missing in sdk: %v \n",
|
|
circuitDetail.CircuitId, circuitDetail.MissingInCtrl, circuitDetail.MissingInForwarder,
|
|
circuitDetail.MissingInEdge, circuitDetail.MissingInSdk)
|
|
}
|
|
if circuitDetail.IsInErrorState() {
|
|
errCount++
|
|
}
|
|
}
|
|
expected--
|
|
}
|
|
}
|
|
fmt.Printf("%v errors found\n", errCount)
|
|
if errCount > 0 {
|
|
os.Exit(1)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (self *validateCircuitsAction) HandleReceive(msg *channel.Message, _ channel.Channel) {
|
|
detail := &mgmt_pb.RouterCircuitDetails{}
|
|
if err := proto.Unmarshal(msg.Body, detail); err != nil {
|
|
pfxlog.Logger().WithError(err).Error("unable to unmarshal router link details")
|
|
return
|
|
}
|
|
|
|
self.eventNotify <- detail
|
|
}
|