Skip to content

Commit

Permalink
Merge pull request #50 from So1S/SO1S-461
Browse files Browse the repository at this point in the history
SO1S-461 Nodes GET API 작성
  • Loading branch information
DPS0340 authored Oct 23, 2022
2 parents 54761e7 + ab94f29 commit 89310a8
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.so1s.backend.domain.kubernetes.controller;

import io.fabric8.kubernetes.api.model.Node;
import io.so1s.backend.domain.kubernetes.dto.response.NodeFindResponseDto;
import io.so1s.backend.domain.kubernetes.service.KubernetesService;
import io.so1s.backend.domain.kubernetes.service.NodesService;
import java.util.List;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/nodes")
@RequiredArgsConstructor
public class NodeController {

private final NodesService nodesService;
private final KubernetesService kubernetesService;

@GetMapping
public ResponseEntity<List<Node>> getNodes() {
return ResponseEntity.ok(nodesService.findNodes());
}

@GetMapping("/{node_name}/yaml")
public ResponseEntity<NodeFindResponseDto> findDeploymentYaml(
@Valid @PathVariable("node_name") String nodeName) {
Node node = nodesService.findNodeByName(nodeName);
String yaml = kubernetesService.getWorkloadToYaml(node);

return ResponseEntity.ok(NodeFindResponseDto.builder().yaml(yaml).build());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.so1s.backend.domain.kubernetes.dto.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class NodeFindResponseDto {

@Builder.Default
private String yaml = "";

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.so1s.backend.domain.kubernetes.exception;

import io.so1s.backend.global.error.exception.NotFoundException;

public class NodeNotFoundException extends NotFoundException {

public NodeNotFoundException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.so1s.backend.domain.kubernetes.service;

import io.fabric8.kubernetes.api.model.Node;
import io.so1s.backend.domain.kubernetes.exception.NodeNotFoundException;
import java.util.List;

public interface NodesService {

List<Node> findNodes();

Node findNodeByName(String name) throws NodeNotFoundException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.so1s.backend.domain.kubernetes.service;

import io.fabric8.kubernetes.api.model.Node;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.so1s.backend.domain.kubernetes.exception.NodeNotFoundException;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class NodesServiceImpl implements NodesService {

private final KubernetesClient client;

@Override
public List<Node> findNodes() {
return client.nodes().list().getItems();
}

@Override
public Node findNodeByName(String name) throws NodeNotFoundException {
return Optional.ofNullable(client.nodes().withName(name).get())
.orElseThrow(() -> new NodeNotFoundException(
String.format("Node %s not found.", name)));
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.so1s.backend.domain.model.exception;

import io.so1s.backend.global.error.exception.DuplicatedException;
import io.so1s.backend.global.error.exception.NotFoundException;

public class DataTypeNotFoundException extends DuplicatedException {
public class DataTypeNotFoundException extends NotFoundException {

public DataTypeNotFoundException(String message) {
super(message);
Expand Down

0 comments on commit 89310a8

Please sign in to comment.