forked from kubeedge/kubeedge
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kubeedge#5572 from luomengY/kead_ctl_client-go
Switch the keadm ctl client to client go and add the restful interface for restart pod.
- Loading branch information
Showing
11 changed files
with
346 additions
and
283 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package common | ||
|
||
type RestartInfo struct { | ||
Namespace string | ||
PodNames []string | ||
} |
48 changes: 48 additions & 0 deletions
48
edge/pkg/metamanager/metaserver/handlerfactory/ext_handler.go
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,48 @@ | ||
package handlerfactory | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/kubeedge/kubeedge/edge/pkg/metamanager/metaserver/common" | ||
) | ||
|
||
func (f *Factory) Restart(namespace string) http.Handler { | ||
h := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
podNameBytes, err := limitedReadBody(req, int64(3*1024*1024)) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
var podNames []string | ||
err = json.Unmarshal(podNameBytes, &podNames) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
restartInfo := common.RestartInfo{ | ||
PodNames: podNames, | ||
Namespace: namespace, | ||
} | ||
restartResponse := f.storage.Restart(req.Context(), restartInfo) | ||
restartResBytes, err := json.Marshal(restartResponse) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
_, err = w.Write(restartResBytes) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
}) | ||
return h | ||
} |
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,67 @@ | ||
/* | ||
Copyright 2024 The KubeEdge Authors. | ||
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 client | ||
|
||
import ( | ||
"context" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
type PodRequest struct { | ||
Namespace string | ||
LabelSelector string | ||
AllNamespaces bool | ||
PodName string | ||
} | ||
|
||
func (podRequest *PodRequest) GetPod(ctx context.Context) (*corev1.Pod, error) { | ||
kubeClient, err := KubeClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
pod, err := kubeClient.CoreV1().Pods(podRequest.Namespace).Get(ctx, podRequest.PodName, metaV1.GetOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return pod, nil | ||
} | ||
|
||
func (podRequest *PodRequest) GetPods(ctx context.Context) (*corev1.PodList, error) { | ||
kubeClient, err := KubeClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if podRequest.AllNamespaces { | ||
podList, err := kubeClient.CoreV1().Pods(metaV1.NamespaceAll).List(ctx, metaV1.ListOptions{ | ||
LabelSelector: podRequest.LabelSelector, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return podList, nil | ||
} | ||
|
||
podList, err := kubeClient.CoreV1().Pods(podRequest.Namespace).List(ctx, metaV1.ListOptions{ | ||
LabelSelector: podRequest.LabelSelector, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return podList, 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,76 @@ | ||
/* | ||
Copyright 2024 The KubeEdge Authors. | ||
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 client | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"k8s.io/client-go/kubernetes" | ||
restclient "k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/clientcmd" | ||
|
||
"github.com/kubeedge/kubeedge/keadm/cmd/keadm/app/cmd/common" | ||
keadutil "github.com/kubeedge/kubeedge/keadm/cmd/keadm/app/cmd/util" | ||
) | ||
|
||
func KubeClient() (*kubernetes.Clientset, error) { | ||
kubeConfig, err := getKubeConfig() | ||
if err != nil { | ||
return nil, err | ||
} | ||
kubeClient, err := kubernetes.NewForConfig(kubeConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return kubeClient, nil | ||
} | ||
|
||
func getKubeConfig() (*restclient.Config, error) { | ||
config, err := keadutil.ParseEdgecoreConfig(common.EdgecoreConfigPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("get edge config failed with err:%v", err) | ||
} | ||
|
||
if !config.Modules.MetaManager.MetaServer.Enable { | ||
return nil, fmt.Errorf("metaserver don't open") | ||
} | ||
|
||
url := config.Modules.MetaManager.MetaServer.Server | ||
ok, requireAuthorization := config.FeatureGates["requireAuthorization"] | ||
if ok && requireAuthorization { | ||
url = "https://" + url | ||
} else { | ||
url = "http://" + url | ||
} | ||
kubeConfig, err := clientcmd.BuildConfigFromFlags(url, "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if ok && requireAuthorization { | ||
serverCrt := config.Modules.MetaManager.MetaServer.TLSCertFile | ||
serverKey := config.Modules.MetaManager.MetaServer.TLSPrivateKeyFile | ||
tlsCaFile := config.Modules.MetaManager.MetaServer.TLSCaFile | ||
|
||
kubeConfig.TLSClientConfig.CAFile = tlsCaFile | ||
kubeConfig.TLSClientConfig.CertFile = serverCrt | ||
kubeConfig.TLSClientConfig.KeyFile = serverKey | ||
} | ||
kubeConfig.Timeout = 1 * time.Minute | ||
return kubeConfig, 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
Oops, something went wrong.