Skip to content

Commit f8da5ce

Browse files
committed
environment(kubernetes): remove docker references
1 parent 4d1d16b commit f8da5ce

File tree

5 files changed

+102
-103
lines changed

5 files changed

+102
-103
lines changed

environment/kubernetes/api.go

+43-45
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package docker
22

33
import (
4-
"context"
5-
"io"
64
"net/http"
75
"reflect"
86
"strings"
@@ -12,7 +10,6 @@ import (
1210
"github.com/docker/docker/api/types"
1311
"github.com/docker/docker/api/types/versions"
1412
"github.com/docker/docker/client"
15-
"github.com/docker/docker/errdefs"
1613
"github.com/goccy/go-json"
1714

1815
"github.com/pterodactyl/wings/config"
@@ -49,49 +46,50 @@ func configure(c *client.Client) {
4946
// a large number of requests to this endpoint are spawned by Wings, and the
5047
// standard "encoding/json" shows its performance woes badly even with single
5148
// containers running.
52-
func (e *Environment) ContainerInspect(ctx context.Context) (types.ContainerJSON, error) {
53-
configure(e.client)
54-
55-
// Support feature flagging of this functionality so that if something goes
56-
// wrong for now it is easy enough for people to switch back to the older method
57-
// of fetching stats.
58-
if !fastEnabled {
59-
return e.client.ContainerInspect(ctx, e.Id)
60-
}
61-
62-
var st types.ContainerJSON
63-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/containers/"+e.Id+"/json", nil)
64-
if err != nil {
65-
return st, errors.WithStack(err)
66-
}
6749

68-
if cli.proto == "unix" || cli.proto == "npipe" {
69-
req.Host = "docker"
70-
}
71-
72-
req.URL.Host = cli.host
73-
req.URL.Scheme = cli.scheme
74-
75-
res, err := e.client.HTTPClient().Do(req)
76-
if err != nil {
77-
if res == nil {
78-
return st, errdefs.Unknown(err)
79-
}
80-
return st, errdefs.FromStatusCode(err, res.StatusCode)
81-
}
82-
83-
body, err := io.ReadAll(res.Body)
84-
if err != nil {
85-
return st, errors.Wrap(err, "failed to read response body from Docker")
86-
}
87-
if err := parseErrorFromResponse(res, body); err != nil {
88-
return st, errdefs.FromStatusCode(err, res.StatusCode)
89-
}
90-
if err := json.Unmarshal(body, &st); err != nil {
91-
return st, errors.WithStack(err)
92-
}
93-
return st, nil
94-
}
50+
// func (e *Environment) ContainerInspect(ctx context.Context) (types.ContainerJSON, error) {
51+
// configure(e.client)
52+
53+
// // Support feature flagging of this functionality so that if something goes
54+
// // wrong for now it is easy enough for people to switch back to the older method
55+
// // of fetching stats.
56+
// if !fastEnabled {
57+
// return e.client.ContainerInspect(ctx, e.Id)
58+
// }
59+
60+
// var st types.ContainerJSON
61+
// req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/containers/"+e.Id+"/json", nil)
62+
// if err != nil {
63+
// return st, errors.WithStack(err)
64+
// }
65+
66+
// if cli.proto == "unix" || cli.proto == "npipe" {
67+
// req.Host = "docker"
68+
// }
69+
70+
// req.URL.Host = cli.host
71+
// req.URL.Scheme = cli.scheme
72+
73+
// res, err := e.client.HTTPClient().Do(req)
74+
// if err != nil {
75+
// if res == nil {
76+
// return st, errdefs.Unknown(err)
77+
// }
78+
// return st, errdefs.FromStatusCode(err, res.StatusCode)
79+
// }
80+
81+
// body, err := io.ReadAll(res.Body)
82+
// if err != nil {
83+
// return st, errors.Wrap(err, "failed to read response body from Docker")
84+
// }
85+
// if err := parseErrorFromResponse(res, body); err != nil {
86+
// return st, errdefs.FromStatusCode(err, res.StatusCode)
87+
// }
88+
// if err := json.Unmarshal(body, &st); err != nil {
89+
// return st, errors.WithStack(err)
90+
// }
91+
// return st, nil
92+
// }
9593

9694
// parseErrorFromResponse is a re-implementation of Docker's
9795
// client.checkResponseErr() function.

environment/kubernetes/container.go

+33-32
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212

1313
"emperror.dev/errors"
1414
"github.com/apex/log"
15-
"github.com/docker/docker/api/types/container"
16-
"github.com/docker/docker/client"
1715
corev1 "k8s.io/api/core/v1"
1816
v1 "k8s.io/api/core/v1"
1917
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -48,7 +46,7 @@ func (e *Environment) isPodRunning(podName, namespace string) wait.ConditionFunc
4846
return func() (bool, error) {
4947
// fmt.Printf(".") // progress bar!
5048

51-
pod, err := e.client2.CoreV1().Pods(config.Get().System.Namespace).Get(context.TODO(), podName, metav1.GetOptions{})
49+
pod, err := e.client.CoreV1().Pods(config.Get().System.Namespace).Get(context.TODO(), podName, metav1.GetOptions{})
5250
if err != nil {
5351
return false, err
5452
}
@@ -119,7 +117,7 @@ func (e *Environment) Attach(ctx context.Context) error {
119117
}
120118
}()
121119

122-
reader := e.client2.CoreV1().Pods(config.Get().System.Namespace).GetLogs(e.Id, &corev1.PodLogOptions{
120+
reader := e.client.CoreV1().Pods(config.Get().System.Namespace).GetLogs(e.Id, &corev1.PodLogOptions{
123121
Follow: true,
124122
})
125123
podLogs, err := reader.Stream(context.TODO())
@@ -145,32 +143,35 @@ func (e *Environment) Attach(ctx context.Context) error {
145143
// limits without actually making any changes to the operational state of the
146144
// container. This allows memory, cpu, and IO limitations to be adjusted on the
147145
// fly for individual instances.
146+
148147
func (e *Environment) InSituUpdate() error {
149-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
150-
defer cancel()
151-
152-
if _, err := e.ContainerInspect(ctx); err != nil {
153-
// If the container doesn't exist for some reason there really isn't anything
154-
// we can do to fix that in this process (it doesn't make sense at least). In those
155-
// cases just return without doing anything since we still want to save the configuration
156-
// to the disk.
157-
//
158-
// We'll let a boot process make modifications to the container if needed at this point.
159-
if client.IsErrNotFound(err) {
160-
return nil
161-
}
162-
return errors.Wrap(err, "environment/docker: could not inspect container")
163-
}
148+
// ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
149+
// defer cancel()
150+
151+
// if _, err := e.ContainerInspect(ctx); err != nil {
152+
// // If the container doesn't exist for some reason there really isn't anything
153+
// // we can do to fix that in this process (it doesn't make sense at least). In those
154+
// // cases just return without doing anything since we still want to save the configuration
155+
// // to the disk.
156+
// //
157+
// // We'll let a boot process make modifications to the container if needed at this point.
158+
// if client.IsErrNotFound(err) {
159+
// return nil
160+
// }
161+
// return errors.Wrap(err, "environment/docker: could not inspect container")
162+
// }
164163

165164
// CPU pinning cannot be removed once it is applied to a container. The same is true
166165
// for removing memory limits, a container must be re-created.
167166
//
168167
// @see https://github.com/moby/moby/issues/41946
169-
if _, err := e.client.ContainerUpdate(ctx, e.Id, container.UpdateConfig{
170-
Resources: e.Configuration.Limits().AsContainerResources(),
171-
}); err != nil {
172-
return errors.Wrap(err, "environment/docker: could not update container")
173-
}
168+
169+
// if _, err := e.client.ContainerUpdate(ctx, e.Id, container.UpdateConfig{
170+
// Resources: e.Configuration.Limits().AsContainerResources(),
171+
// }); err != nil {
172+
//
173+
// return errors.Wrap(err, "environment/docker: could not update container")
174+
// }
174175
return nil
175176
}
176177

@@ -183,7 +184,7 @@ func (e *Environment) Create() error {
183184
// If the pod already exists don't hit the user with an error, just return
184185
// the current information about it which is what we would do when creating the
185186
// pod anyways.
186-
if _, err := e.client2.CoreV1().Pods(config.Get().System.Namespace).Get(ctx, e.Id, metav1.GetOptions{}); err == nil {
187+
if _, err := e.client.CoreV1().Pods(config.Get().System.Namespace).Get(ctx, e.Id, metav1.GetOptions{}); err == nil {
187188
return nil
188189
} else if !apierrors.IsNotFound(err) {
189190
return errors.Wrap(err, "environment/kubernetes: failed to inspect pod")
@@ -296,7 +297,7 @@ func (e *Environment) Create() error {
296297
// conf.User = strconv.Itoa(cfg.System.User.Uid) + ":" + strconv.Itoa(cfg.System.User.Gid)
297298
// }
298299

299-
if _, err := e.client2.CoreV1().Pods(config.Get().System.Namespace).Create(ctx, pod, metav1.CreateOptions{}); err != nil {
300+
if _, err := e.client.CoreV1().Pods(config.Get().System.Namespace).Create(ctx, pod, metav1.CreateOptions{}); err != nil {
300301
return errors.Wrap(err, "environment/kubernetes: failed to create pod")
301302
}
302303

@@ -327,7 +328,7 @@ func (e *Environment) Create() error {
327328
service.Spec.Ports = append(service.Spec.Ports, corev1.ServicePort{Name: b.Proto() + b.Port(), Protocol: corev1.Protocol(protocol), Port: int32(port)})
328329
}
329330

330-
if _, err := e.client2.CoreV1().Services(config.Get().System.Namespace).Create(ctx, service, metav1.CreateOptions{}); err != nil {
331+
if _, err := e.client.CoreV1().Services(config.Get().System.Namespace).Create(ctx, service, metav1.CreateOptions{}); err != nil {
331332
if !apierrors.IsAlreadyExists(err) {
332333
return errors.Wrap(err, "environment/kubernetes: failed to create service")
333334
}
@@ -346,19 +347,19 @@ func (e *Environment) Destroy() error {
346347
policy := metav1.DeletePropagationForeground
347348

348349
// Delete Pod
349-
err := e.client2.CoreV1().Pods(config.Get().System.Namespace).Delete(context.Background(), e.Id, metav1.DeleteOptions{GracePeriodSeconds: &zero, PropagationPolicy: &policy})
350+
err := e.client.CoreV1().Pods(config.Get().System.Namespace).Delete(context.Background(), e.Id, metav1.DeleteOptions{GracePeriodSeconds: &zero, PropagationPolicy: &policy})
350351
if err != nil && !apierrors.IsNotFound(err) {
351352
return err
352353
}
353354

354355
// Delete Service
355-
err = e.client2.CoreV1().Services(config.Get().System.Namespace).Delete(context.Background(), "svc-"+e.Id, metav1.DeleteOptions{GracePeriodSeconds: &zero, PropagationPolicy: &policy})
356+
err = e.client.CoreV1().Services(config.Get().System.Namespace).Delete(context.Background(), "svc-"+e.Id, metav1.DeleteOptions{GracePeriodSeconds: &zero, PropagationPolicy: &policy})
356357
if err != nil && !apierrors.IsNotFound(err) {
357358
return err
358359
}
359360

360361
// Delete PVC
361-
err = e.client2.CoreV1().PersistentVolumeClaims(config.Get().System.Namespace).Delete(context.Background(), e.Id+"-pvc", metav1.DeleteOptions{GracePeriodSeconds: &zero, PropagationPolicy: &policy})
362+
err = e.client.CoreV1().PersistentVolumeClaims(config.Get().System.Namespace).Delete(context.Background(), e.Id+"-pvc", metav1.DeleteOptions{GracePeriodSeconds: &zero, PropagationPolicy: &policy})
362363
if err != nil && !apierrors.IsNotFound(err) {
363364
return err
364365
}
@@ -386,7 +387,7 @@ func (e *Environment) SendCommand(c string) error {
386387
e.SetState(environment.ProcessStoppingState)
387388
}
388389

389-
req := e.client2.CoreV1().RESTClient().
390+
req := e.client.CoreV1().RESTClient().
390391
Post().
391392
Namespace(config.Get().System.Namespace).
392393
Resource("pods").
@@ -428,7 +429,7 @@ func (e *Environment) SendCommand(c string) error {
428429
// is running or not, it will simply try to read the last X bytes of the file
429430
// and return them.
430431
func (e *Environment) Readlog(lines int) ([]string, error) {
431-
r := e.client2.CoreV1().Pods(config.Get().System.Namespace).GetLogs(e.Id, &corev1.PodLogOptions{
432+
r := e.client.CoreV1().Pods(config.Get().System.Namespace).GetLogs(e.Id, &corev1.PodLogOptions{
432433
TailLines: &[]int64{int64(lines)}[0],
433434
})
434435
podLogs, err := r.Stream(context.Background())

environment/kubernetes/environment.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"emperror.dev/errors"
99
"github.com/apex/log"
1010
"github.com/docker/docker/api/types"
11-
"github.com/docker/docker/client"
1211
v1 "k8s.io/api/core/v1"
12+
apierrors "k8s.io/apimachinery/pkg/api/errors"
1313
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1414
"k8s.io/client-go/kubernetes"
1515
"k8s.io/client-go/rest"
@@ -43,9 +43,8 @@ type Environment struct {
4343
meta *Metadata
4444

4545
// The Docker client being used for this instance.
46-
client *client.Client
47-
config *rest.Config
48-
client2 *kubernetes.Clientset
46+
config *rest.Config
47+
client *kubernetes.Clientset
4948

5049
// Controls the hijacked response stream which exists only when we're attached to
5150
// the running container instance.
@@ -68,12 +67,7 @@ type Environment struct {
6867
// unique per-server (we use the UUID by default). The container does not need
6968
// to exist at this point.
7069
func New(id string, m *Metadata, c *environment.Configuration) (*Environment, error) {
71-
cli, err := environment.Docker()
72-
if err != nil {
73-
return nil, err
74-
}
75-
76-
config, cli2, err := environment.Kubernetes()
70+
config, cli, err := environment.Kubernetes()
7771
if err != nil {
7872
return nil, err
7973
}
@@ -82,9 +76,8 @@ func New(id string, m *Metadata, c *environment.Configuration) (*Environment, er
8276
Id: id,
8377
Configuration: c,
8478
meta: m,
85-
client: cli,
8679
config: config,
87-
client2: cli2,
80+
client: cli,
8881
st: system.NewAtomicString(environment.ProcessOfflineState),
8982
emitter: events.NewBus(),
9083
}
@@ -128,11 +121,11 @@ func (e *Environment) Events() *events.Bus {
128121
// name as the lookup parameter in addition to the longer ID auto-assigned when
129122
// the container is created.
130123
func (e *Environment) Exists() (bool, error) {
131-
_, err := e.ContainerInspect(context.Background())
124+
_, err := e.client.CoreV1().Pods(config.Get().System.Namespace).Get(context.Background(), e.Id, metav1.GetOptions{})
132125
if err != nil {
133126
// If this error is because the container instance wasn't found via Docker we
134127
// can safely ignore the error and just return false.
135-
if client.IsErrNotFound(err) {
128+
if apierrors.IsNotFound(err) {
136129
return false, nil
137130
}
138131
return false, err
@@ -150,7 +143,7 @@ func (e *Environment) Exists() (bool, error) {
150143
//
151144
// @see docker/client/errors.go
152145
func (e *Environment) IsRunning(ctx context.Context) (bool, error) {
153-
c, err := e.client2.CoreV1().Pods(config.Get().System.Namespace).Get(ctx, e.Id, metav1.GetOptions{})
146+
c, err := e.client.CoreV1().Pods(config.Get().System.Namespace).Get(ctx, e.Id, metav1.GetOptions{})
154147
if err != nil {
155148
return false, err
156149
}
@@ -164,7 +157,7 @@ func (e *Environment) IsRunning(ctx context.Context) (bool, error) {
164157
// ExitState returns the container exit state, the exit code and whether or not
165158
// the container was killed by the OOM killer.
166159
func (e *Environment) ExitState() (uint32, bool, error) {
167-
c, err := e.ContainerInspect(context.Background())
160+
c, err := e.client.CoreV1().Pods(config.Get().System.Namespace).Get(context.Background(), e.Id, metav1.GetOptions{})
168161
if err != nil {
169162
// I'm not entirely sure how this can happen to be honest. I tried deleting a
170163
// container _while_ a server was running and wings gracefully saw the crash and
@@ -175,12 +168,19 @@ func (e *Environment) ExitState() (uint32, bool, error) {
175168
// so that's a mystery that will have to go unsolved.
176169
//
177170
// @see https://github.com/pterodactyl/panel/issues/2003
178-
if client.IsErrNotFound(err) {
171+
if apierrors.IsNotFound(err) {
179172
return 1, false, nil
180173
}
181174
return 0, false, err
182175
}
183-
return uint32(c.State.ExitCode), c.State.OOMKilled, nil
176+
177+
if c.Status.Phase != v1.PodRunning {
178+
if c.Status.ContainerStatuses[0].State.Terminated.ExitCode == 137 {
179+
return 137, true, nil
180+
}
181+
return uint32(c.Status.ContainerStatuses[0].State.Terminated.ExitCode), false, nil
182+
}
183+
return 1, false, nil
184184
}
185185

186186
// Config returns the environment configuration allowing a process to make

0 commit comments

Comments
 (0)