Skip to content

Commit

Permalink
refactored core component handler
Browse files Browse the repository at this point in the history
Signed-off-by: Utkarsh Srivastava <[email protected]>
  • Loading branch information
tangledbytes committed Jun 17, 2021
1 parent 41fe8e5 commit 8955835
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 38 deletions.
12 changes: 6 additions & 6 deletions istio/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ var (
// generated when kubernetes client is nil
ErrNilClientCode = "istio_test_code"

// ErrParseVirtualServiceCode represents the error code which is
// generated when virtual service parsing fails
ErrParseVirtualServiceCode = "istio_test_code"
// ErrParseIstioCoreComponentCode represents the error code which is
// generated when istio core component manifest parsing fails
ErrParseIstioCoreComponentCode = "istio_test_code"

// ErrInvalidOAMComponentTypeCode represents the error code which is
// generated when an invalid oam component is requested
Expand Down Expand Up @@ -191,9 +191,9 @@ func ErrIstioVet(err error) error {
return errors.NewDefault(ErrIstioVetCode, err.Error())
}

// ErrParseVirtualService is the error when vsc parsing fails
func ErrParseVirtualService(err error) error {
return errors.NewDefault(ErrParseVirtualServiceCode, err.Error())
// ErrParseIstioCoreComponent is the error when istio core component manifest parsing fails
func ErrParseIstioCoreComponent(err error) error {
return errors.NewDefault(ErrParseIstioCoreComponentCode, err.Error())
}

// ErrInvalidOAMComponentType is the error when the OAM component name is not valid
Expand Down
49 changes: 17 additions & 32 deletions istio/oam.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,37 +118,22 @@ func handleComponentIstioMesh(istio *Istio, comp v1alpha1.Component, isDel bool)
}

func handleComponentVirtualService(istio *Istio, comp v1alpha1.Component, isDel bool) (string, error) {
virtualSvc := map[string]interface{}{
"apiVersion": "networking.istio.io/v1beta1",
"kind": "VirtualService",
"metadata": map[string]interface{}{
"name": comp.Name,
"annotations": comp.Annotations,
"labels": comp.Labels,
},
"spec": comp.Spec.Settings,
}

// Convert to yaml
yamlByt, err := yaml.Marshal(virtualSvc)
if err != nil {
err = ErrParseVirtualService(err)
istio.Log.Error(err)
return "", err
}

msg := fmt.Sprintf("created virtual service \"%s\" in namespace \"%s\"", comp.Name, comp.Namespace)
if isDel {
msg = fmt.Sprintf("deleted virtual service \"%s\" in namespace \"%s\"", comp.Name, comp.Namespace)
}

return msg, istio.applyManifest(yamlByt, isDel, comp.Namespace)
return handleIstioCoreComponent(istio, comp, isDel, "networking.istio.io/v1beta1", "VirtualService")
}

func handleComponentEnvoyFilter(istio *Istio, comp v1alpha1.Component, isDel bool) (string, error) {
envoyFilter := map[string]interface{}{
"apiVersion": "networking.istio.io/v1alpha3",
"kind": "EnvoyFilter",
return handleIstioCoreComponent(istio, comp, isDel, "networking.istio.io/v1alpha3", "EnvoyFilter")
}

func handleIstioCoreComponent(
istio *Istio,
comp v1alpha1.Component,
isDel bool,
apiVersion,
kind string) (string, error) {
component := map[string]interface{}{
"apiVersion": apiVersion,
"kind": kind,
"metadata": map[string]interface{}{
"name": comp.Name,
"annotations": comp.Annotations,
Expand All @@ -158,16 +143,16 @@ func handleComponentEnvoyFilter(istio *Istio, comp v1alpha1.Component, isDel boo
}

// Convert to yaml
yamlByt, err := yaml.Marshal(envoyFilter)
yamlByt, err := yaml.Marshal(component)
if err != nil {
err = ErrParseVirtualService(err)
err = ErrParseIstioCoreComponent(err)
istio.Log.Error(err)
return "", err
}

msg := fmt.Sprintf("configured envoy filter \"%s\" in namespace \"%s\"", comp.Name, comp.Namespace)
msg := fmt.Sprintf("created %s \"%s\" in namespace \"%s\"", kind, comp.Name, comp.Namespace)
if isDel {
msg = fmt.Sprintf("deleted envoy filter config \"%s\" in namespace \"%s\"", comp.Name, comp.Namespace)
msg = fmt.Sprintf("deleted %s config \"%s\" in namespace \"%s\"", kind, comp.Name, comp.Namespace)
}

return msg, istio.applyManifest(yamlByt, isDel, comp.Namespace)
Expand Down

0 comments on commit 8955835

Please sign in to comment.