@@ -15,13 +15,17 @@ package scaffold
1515import (
1616 "bytes"
1717 "context"
18- "encoding/json "
18+ "errors "
1919 "os"
2020 "os/exec"
2121 "time"
2222
23+ "go.uber.org/zap"
24+ "gopkg.in/yaml.v3"
25+
2326 adctypes "github.com/apache/apisix-ingress-controller/api/adc"
2427 "github.com/apache/apisix-ingress-controller/internal/provider/adc/translator"
28+ "github.com/api7/gopkg/pkg/log"
2529)
2630
2731// DataplaneResource defines the interface for accessing dataplane resources
@@ -88,6 +92,19 @@ func (a *adcDataplaneResource) Consumer() ConsumerResource {
8892 return & adcConsumerResource {a }
8993}
9094
95+ func init () {
96+ // dashboard sdk log
97+ l , err := log .NewLogger (
98+ log .WithOutputFile ("stderr" ),
99+ log .WithLogLevel ("debug" ),
100+ log .WithSkipFrames (3 ),
101+ )
102+ if err != nil {
103+ panic (err )
104+ }
105+ log .DefaultLogger = l
106+ }
107+
91108// dumpResources executes adc dump command and returns the resources
92109func (a * adcDataplaneResource ) dumpResources (ctx context.Context ) (* translator.TranslateResult , error ) {
93110 ctxWithTimeout , cancel := context .WithTimeout (ctx , a .syncTimeout )
@@ -99,6 +116,7 @@ func (a *adcDataplaneResource) dumpResources(ctx context.Context) (*translator.T
99116 }
100117
101118 adcEnv := []string {
119+ "ADC_RUNNING_MODE=" , // need to set empty
102120 "ADC_BACKEND=" + a .backend ,
103121 "ADC_SERVER=" + a .serverAddr ,
104122 "ADC_TOKEN=" + a .token ,
@@ -111,12 +129,31 @@ func (a *adcDataplaneResource) dumpResources(ctx context.Context) (*translator.T
111129 cmd .Env = append (cmd .Env , os .Environ ()... )
112130 cmd .Env = append (cmd .Env , adcEnv ... )
113131
132+ log .Debug ("running adc command" , zap .String ("command" , cmd .String ()), zap .Strings ("env" , adcEnv ))
133+
114134 if err := cmd .Run (); err != nil {
135+ stderrStr := stderr .String ()
136+ stdoutStr := stdout .String ()
137+ errMsg := stderrStr
138+ if errMsg == "" {
139+ errMsg = stdoutStr
140+ }
141+ log .Errorw ("failed to run adc" ,
142+ zap .Error (err ),
143+ zap .String ("output" , stdoutStr ),
144+ zap .String ("stderr" , stderrStr ),
145+ )
146+ return nil , errors .New ("failed to dump resources: " + errMsg + ", exit err: " + err .Error ())
147+ }
148+
149+ // Read the YAML file that was created by adc dump
150+ yamlData , err := os .ReadFile ("/tmp/dump.yaml" )
151+ if err != nil {
115152 return nil , err
116153 }
117154
118155 var resources adctypes.Resources
119- if err := json .Unmarshal (stdout . Bytes () , & resources ); err != nil {
156+ if err := yaml .Unmarshal (yamlData , & resources ); err != nil {
120157 return nil , err
121158 }
122159
0 commit comments