-
Notifications
You must be signed in to change notification settings - Fork 612
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
115 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/go-resty/resty/v2" | ||
"golang.org/x/net/html" | ||
) | ||
|
||
func (k *K8sApiReference) GetApiDoc(field string) (string, error) { | ||
client := resty.New() | ||
splitApiVersion := strings.Split( | ||
strings.ToLower(k.ApiVersion), | ||
"/", | ||
) | ||
|
||
resp, err := client.R(). | ||
EnableTrace(). | ||
Get( | ||
fmt.Sprintf( | ||
"https://kubernetes.io/docs/reference/generated/kubernetes-api/v%s.%s/#%sspec-%s-%s", | ||
k.ServerVersion.Major, | ||
k.ServerVersion.Minor, | ||
strings.ToLower(k.Kind), | ||
splitApiVersion[1], | ||
splitApiVersion[0], | ||
), | ||
) | ||
if err != nil { | ||
fmt.Printf("%w", err) | ||
} | ||
|
||
fieldDoc := "" | ||
isFound := false | ||
for _, line := range strings.Split(strings.TrimRight(string(resp.Body()), "\n"), "\n") { | ||
if strings.Contains(line, field) { | ||
tkn := html.NewTokenizer(strings.NewReader(line)) | ||
isTd := false | ||
|
||
for { | ||
tt := tkn.Next() | ||
|
||
switch tt { | ||
case html.ErrorToken: | ||
break | ||
|
||
case html.StartTagToken: | ||
t := tkn.Token() | ||
isTd = t.Data == "td" | ||
|
||
case html.TextToken: | ||
t := tkn.Token() | ||
|
||
if isTd && t.Data != field { | ||
fieldDoc = t.Data | ||
isFound = true | ||
} | ||
|
||
isTd = false | ||
} | ||
|
||
if isFound { | ||
break | ||
} | ||
} | ||
} | ||
|
||
if isFound { | ||
break | ||
} | ||
} | ||
|
||
return fieldDoc, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package util | ||
|
||
import "k8s.io/apimachinery/pkg/version" | ||
|
||
type K8sApiReference struct { | ||
ApiVersion string | ||
Kind string | ||
ServerVersion *version.Info | ||
} |